Getting the values of checkbox in new activity android - android-activity

I need a little bit help regarding retrieval of values which are selected or ticked in the Checkbox list. Once the user clicks on confirm button, the selected items must appear in new activity.
I'm very basic to android, so needs a little assistance.
FoodMainActivity.java
package com.example.sailalith.sample1;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.Toast;
public class FoodMainActivity extends ListActivity {
String[] city= {
"Indian bread : Rumali roti - Rs. 20",
"plain naan - Rs. 15",
"Paneer Butter Masala - Rs. 120",
"Mushroom Curry - Rs. 100",
"Jeera rice - Rs. 120",
"Egg Masala - Rs. 80",
"Green peas masala - Rs. 60",
"Veg. Biryani - Rs. 120",
"Dal fry - Rs. 90",
"Malai Koftha curry - Rs. 120",
"Veg. Fried rice - Rs. 80",
"Veg. Pulav - Rs. 70",
" "
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.food_main);
// -- Display mode of the ListView
Button btn= (Button) findViewById(R.id.testbutton);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
ListView listview= getListView();
// listview.setChoiceMode(listview.CHOICE_MODE_NONE);
// listview.setChoiceMode(listview.CHOICE_MODE_SINGLE);
listview.setChoiceMode(listview.CHOICE_MODE_MULTIPLE);
//-- text filtering
listview.setTextFilterEnabled(true);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked,city));
}
public void onListItemClick(ListView parent, View v, int position, long id){
CheckedTextView item = (CheckedTextView) v;
Toast.makeText(this, city[position] + " checked : " +
item.isChecked(), Toast.LENGTH_SHORT).show();
}
}
XML file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
</ListView>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/testbutton"
android:text="Confirm"
android:layout_alignParentBottom="true" />
</RelativeLayout>

In onListItemClick() function, you are getting position of the selected option in the listview. You can retrieve the selected item using position.
Just add this line in onListItemClick() function :
String str = listview.getItemAtPosition(position).toString();

Related

Load Webview in second Fragment when Button is clicked from First Fragment

I have two Fragments. First Fragment is named as Fragment1 and Second Fragment named as physics. First Fragment have Two Buttons. I would Like to show a local html page ( say A.html when Button1 is pressed and B.html when Button2 is Pressed ) in Second Fragment i.e in physics.java
My Fragment.java contains following code
package com.nepalpolice.mnemonics;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* Created by Sagar on 2017/09/23.
*/
public class Fragment1 extends Fragment implements View.OnClickListener{
public Button button;
public Fragment1() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
button = (Button) view.findViewById(R.id.button1);
button = (Button) view.findViewById(R.id.button2);
return view;
}
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity().getApplicationContext(), physics.class);
switch (v.getId()) {
case R.id.button1:
intent.putExtra("url", "file:///android_asset/B.html");
startActivity(intent);
break;
case R.id.button2:
intent.putExtra("url", "file:///android_asset/A.html");
startActivity(intent);
break;
default:
break;
}
}
}
and My SecondFragment contains following code
package com.nepalpolice.mnemonics;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
* Created by Sagar on 2017/09/23.
*/
public class physics extends Fragment {
WebView myWebView;
public physics() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_fragment2, container, false);
String url = getActivity().getIntent().getStringExtra("url");
myWebView=(WebView)rootView.findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl(url);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
return rootView;
}
}
and main fragment_fragment1.xml contains
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.nepalpolice.mnemonics.Fragment1">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentTop="true"
android:text="button01"
android:onClick="onClick"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentTop="true"
android:text="button02"
android:onClick="onClick"/>
</LinearLayout>
</FrameLayout>
and Secondfragment.xml has
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="#+id/webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
But my is Crashing with following Error.
java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button1'
I'm almost Done with my project. If it could be done...It would mean a lot for me.

How to change widget view in background service

I have created widget. I am trying to update the widget in background each 5 seconds (For testing purpose 5 second). But it is not getting update.Is any mistake on my code.
Provider class
package com.widget.widgetapplication;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
import java.util.Random;
public class MyWidget extends AppWidgetProvider {
private int randomNumber = 0;
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Toast.makeText(context, "Receive", Toast.LENGTH_SHORT).show();
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Toast.makeText(context, "Update", Toast.LENGTH_SHORT).show();
// Get all ids
ComponentName thisWidget = new ComponentName(context,
MyWidget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
// create some random data
randomNumber = new Random().nextInt(100);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
Log.w("WidgetExample", String.valueOf(randomNumber));
// Set the text
remoteViews.setTextViewText(R.id.time, String.valueOf(randomNumber));
appWidgetManager.notifyAppWidgetViewDataChanged(widgetId, R.id.time);
// Register an onClickListener
Intent intent = new Intent(context, MyWidget.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.watch, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
}
widget info xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="100dp"
android:minWidth="100dp"
android:previewImage="#drawable/analog_watch"
android:initialLayout="#layout/widget_layout"
android:updatePeriodMillis="5000">
</appwidget-provide
Registered receiver in manifest
<receiver android:name=".MyWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="#xml/widget_info"/>
</receiver>
Widget layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent">
<AnalogClock
android:id="#+id/watch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textStyle="bold"
android:text="Time is : "
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"/>
</LinearLayout>
YES. you are doing it wrong.
the minimum available value for updatePeriodMillis is 30min (1800000 millis).
official document :
Note: Updates requested with updatePeriodMillis will not be delivered more than once every 30 minutes.

Call an activity using Alarm Manager & Broadcast Receiver

I'm trying to create an app that automatically make phone calls after regular intervals to a specified number. I'm using Alarm Manager and Broadcast Receiver for this purpose. Alarm Manager can't initiate PHONE CALL activity and application terminates giving an error.
Here's my code. I am new in this Dev this.
*MainActivity.java
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private TextView label;
private EditText phoneNum;
private PendingIntent pendingIntent;
private AlarmManager manager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
label=(TextView)findViewById(R.id.entertextlabel);
phoneNum=(EditText)findViewById(R.id.phonenofield);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
//pendingIntent = PendingIntent.getActivity(this,1,alarmIntent,0);
}
public void startAlarm(View view) {
manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
int interval = 10000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
public void cancelAlarm(View view) {
if (manager != null) {
manager.cancel(pendingIntent);
Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}
}
}
*AlarmReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;
/**
* Created by Saud on 05/11/2015.
*/
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent arg1) {
// For our recurring task, we'll just display a message
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
Intent in = new Intent(Intent.ACTION_CALL);
in.setData(Uri.parse("tel:03324310929"));
context.startActivity(in);
}
}
I'm getting error "Call requires user permission...."
at
context.startActivity(in);
*mainactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Enter the Mobile number"
android:id="#+id/entertextlabel"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Alarm"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_toStartOf="#+id/callBtn"
android:layout_marginBottom="81dp"
android:onClick="startAlarm"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel Alarm"
android:id="#+id/button2"
android:layout_alignTop="#+id/button"
android:layout_toEndOf="#+id/callBtn"
android:onClick="cancelAlarm"/>
</RelativeLayout>
*Mantifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.saud.nexustelecom" >
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<receiver android:name=".AlarmReceiver"></receiver>
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The issue is resolved.
I used the Flag "FLAG_ACTIVITY_NEW_TASK" with with the intent in AlarmReceiver class.
New code is as follows
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;
/**
* Created by Saud on 05/11/2015.
*/
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent arg1) {
// For our recurring task, we'll just display a message
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
// Intent in = new Intent(Intent.ACTION_CALL);
//in.setData(Uri.parse("tel:03324310929"));
Intent in = new Intent(Intent.ACTION_CALL);
in.setData(Uri.parse("tel:03324310929"));
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(in);
}
}

Adding image and childitem in a listview in android

I'm a beginner to android n m tryin to display a listview wer i'll have an image, items n subitems(childitem).. i tried it but m unable to c the image and the subitem of the list.. here's my code :-
activity_main :-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
listview_layout:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/hello"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
/>
<TextView
android:id="#+id/cur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
/>
</LinearLayout>
MainActivity :-
public class MainActivity extends Activity {
// Array of strings storing country names
String[] countries = new String[] {
"India",
"Pakistan",
"Sri Lanka",
"China",
"Bangladesh",
"Nepal",
"Afghanistan",
"North Korea",
"South Korea",
"Japan"
};
// Array of integers points to images stored in /res/drawable-ldpi/
int[] flags = new int[]{
R.drawable.india,
R.drawable.pakistan,
R.drawable.srilanka,
R.drawable.china,
R.drawable.bangladesh,
R.drawable.nepal,
R.drawable.afghanistan,
R.drawable.nkorea,
R.drawable.skorea,
R.drawable.japan
};
// Array of strings to store currencies
String[] currency = new String[]{
"Indian Rupee",
"Pakistani Rupee",
"Sri Lankan Rupee",
"Renminbi",
"Bangladeshi Taka",
"Nepalese Rupee",
"Afghani",
"North Korean Won",
"South Korean Won",
"Japanese Yen"
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<10;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Country : " + countries[i]);
hm.put("cur","Currency : " + currency[i]);
hm.put("flag", Integer.toString(flags[i]) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur" };
// Ids of views in listview_layout
int[] to = { R.id.listview,R.id.txt,R.id.cur};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = ( ListView ) findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
// Item Click Listener for the listview
OnItemClickListener itemClickListener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View container, int position, long id) {
// Getting the Container Layout of the ListView
LinearLayout linearLayoutParent = (LinearLayout) container;
// Getting the inner Linear Layout
LinearLayout linearLayoutChild = (LinearLayout ) linearLayoutParent.getChildAt(1);
// Getting the Country TextView
TextView tvCountry = (TextView) linearLayoutChild.getChildAt(0);
Toast.makeText(getBaseContext(), tvCountry.getText().toString(), Toast.LENGTH_SHORT).show();
}
};
// Setting the item click listener for the listview
listView.setOnItemClickListener(itemClickListener);
}
In the output the listview just displays the country name.. the image and the currency values are not displayed.. wht m i missin ?

pass values of clicked item in item list to a new activity

I am trying to get the values clicked on my listactivity to display on the third.xml. it has 4 editText fields. I want to display the 4 columns on the 4 ET fields. HELP!!!
HEre is a code for getting a specific record in my DBAdapter:
public Cursor getRecord(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {ROW_ID,
KEY_FIRSTNAME, KEY_LASTNAME, KEY_CITY, KEY_STATE},
ROW_ID + "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
Then here is my Second.java list acitivity
package com.dtan.testcontacts;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
public class Second extends ListActivity {
public final static String ID_EXTRA="com.dtan.testcontacts._ID";
DBAdapter db = new DBAdapter(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db.open();
Cursor c = db.getAllContacts();
ArrayList<String> columns = new ArrayList<String>();
int iFirstName = c.getColumnIndex(DBAdapter.KEY_FIRSTNAME);
int iLastName = c.getColumnIndex(DBAdapter.KEY_LASTNAME);
for(c.move(0); c.moveToNext(); c.isAfterLast()){
String columnstring = c.getString(iFirstName)+ " " + c.getString(iLastName)+"\n";
columns.add(columnstring);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, columns);
setListAdapter(adapter);
final ListView lv = getListView();
registerForContextMenu(lv);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextmenu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()){
case R.id.delete:
AlertDialog.Builder abuilder = new AlertDialog.Builder(Second.this);
abuilder.setMessage("Are you sure you want to delete?");
abuilder.setCancelable(false);
abuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Cursor c = db.getAllContacts();
c.moveToPosition(info.position);
String id = c.getString(c.getColumnIndex(DBAdapter.ROW_ID));
db.open();
db.deleteContact(Long.parseLong(id));
c.close();
Toast toast = Toast.makeText(Second.this, "Contact Deleted Successfully", 5000);
toast.show();
Intent i = new Intent(Second.this, Second.class);
startActivity(i);
}
});
abuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = abuilder.create();
alert.show();
return true;
case R.id.edit:
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> lv, View cell,
int position, long id) {
Intent i = new Intent(Second.this, Third.class);
i.putExtra("entry_id", position);
startActivity(i);
}
});
}
return super.onContextItemSelected(item);
}
}
Finally, here is the Third activity where i want to display the value clicked on the listactivity to display on each edittext. xml first then java code:
<EditText
android:id="#+id/editText1Third"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:clickable="false"
android:ems="10"
android:hint="Firstname"
android:inputType="text"
android:text="#string/none" />
<TextView
android:id="#+id/textView1"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:text="#string/fname" />
<EditText
android:id="#+id/editText2Third"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Lastname"
android:inputType="text"
android:text="#string/none" />
<TextView
android:id="#+id/textView2"
android:layout_width="74dp"
android:layout_height="wrap_content"
android:text="#string/lastname" />
<EditText
android:id="#+id/editText3Third"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="City"
android:inputType="text"
android:text="#string/none" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/city" />
<EditText
android:id="#+id/editText4Third"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="State"
android:inputType="text"
android:text="#string/none" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/state" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="64dp" >
<Button
android:id="#+id/ButtonUpdateThird"
android:layout_width="100dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="#string/update" />
</RelativeLayout>
</LinearLayout>
Third.java
package com.dtan.testcontacts;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.EditText;
public class Third extends Activity {
private DBAdapter db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
int b = this.getIntent().getExtras().getInt("entry_id");
EditText firstname = (EditText) findViewById(R.id.editText1Third);
db.open();
Cursor c = db.getRecord(b);
firstname.setText(c.getString(1));
db.close();
}
}