I have created a ListView where I have to add different items. Now, when I click on a particular item it displays another window. On that window, I want to display the name of that item which I click on the ListView.
My code:
private ListView contactList;
private String lv_arr[]={"Android","iPhone","BlackBerry","AndroidPeople"};
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.contact_activity);
contactList=(ListView)findViewById(R.id.ListView01);
contactList.setAdapter(new ArrayAdapter<String (this,android.R.layout.simple_list_item_1 , lv_arr));
contactList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/*Intent myIntent = new Intent(view.getContext(), CallActivity.class);
startActivity(myIntent);*/
}
});
}
you have to get the name of the item on itemclick event. pass it to the activity which will be called. in in the calling activity get the name of item and display
contactList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String selecteditem = lv_arr[position];
Intent myIntent = new Intent(view.getContext(), CallActivity.class);
intent.putExtra("item", selecteditem);
startActivity(myIntent);
}
});
In CallActivity.java
write the following to get the selected item name
String selectedItem=getIntent().getStringExtra("item");
Instead of starting a new activity, use AlertDialog. You already have the position of the list item clicked. So displaying it on the dialog shouldn't be a problem if you follow the article linked.
EDIT :
As per your requirement, you have to launch a new activity to display a string
In the sending list activity
intent.putExtra(String key, String value)
In receiving activity,
String value = getIntent().getStringExtra(key);
Related
I want to get the item on list view to edit Text in another activity.
When clicked on list view item, I want to transfer the item in another activity in edit Text.
You have to make onItemClickListner of listview like that.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);
}
});
Then in the new Activity, retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
And finally set Value to editText like this
editText.setText(value);
Hope this will help you.
lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getActivity(), NewActivity.class);
intent.putExtra("text", text want to transfer);
startActivity(intent);
}
});
You can make use of SharedPreferences. And when you pass the content of the ListView to the next activity, you can use editText.setText("Your Text").
You can also pass your data through intents from which you are calling your new activity.
create onClick method like this.
ListView list = (ListView) findViewById(R.id.newsList);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long offset) {
NewsItem item = (NewsItem) adapter.getItem(position);
Intent intent = new Intent(getApplicationContext(), NewsDetailsActivity.class);
intent.putExtra(KEY, item.getHeadline());
startActivity(intent);
}
});
In next activity
Intent intent = getIntent();
headline = intent.getStringExtra(KEY);
have a look here
I have a listview which is populated by an array adpater. A user starts on activity a and after clicking an item in the listview is taken to activity b where they can edit the item they clicked. The edited item from B is sent back to A through an intent after the user returns to A. I want the original item clicked in A to be replaced with the data received from the intent from B.
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
// setListAdapter(adapter);
final ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
//intent receiving data from Activity B; I want to add in statement to change the original item clicked to this new String, edit;
Bundle fromedit = getIntent().getExtras();
if (fromedit != null) {
String edit = fromedit.getString("BENG");
int ps = fromedit.getInt("POSITION");
}
//onclicklistener for the listview
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(Notes.this, EditNote.class);
intent.putExtra("KEY", values.get(position).toString());
intent.putExtra("POSITION", position);
startActivity(intent);
}
});
Thanks!
In your original activity you need to keep an int variable referring to the selected item index on the list. So your onItemClick() would look like that:
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
currentSelected = position;
Intent intent = new Intent(Notes.this, EditNote.class);
intent.putExtra("KEY", values.get(position).toString());
intent.putExtra("POSITION", position);
startActivity(intent);
}
When you return from the activity where you modify the data you need to update the item of your values list and call adapter.notifyDataSetChanged().
Have you tried to update your list by calling mList.set(index, Object); and then refreshing the Adapter to update ListView displayed data?
You can use the ArrayList#set() method, thus if your 'values' dataset is an ArrayList.
The java.util.ArrayList.set(int index, E element) replaces the element at the specified position in this list with the specified element.
modify your code as follows:
//intent receiving data from Activity B; I want to add in statement to change the original item clicked to this new String, edit;
Bundle fromedit = getIntent().getExtras();
if (fromedit != null) {
String edit = fromedit.getString("BENG");
int ps = fromedit.getInt("POSITION");
values.set(ps, edit);
adapter.notifyDataSetChanged();
}
I've an Activity containing a basic Listview. The functionality works as follows:
click a listview item -> get the listview item name -> pass the name with intent and start a new activity.
Everything is working except for the start of a new Activity. Its just not responsive and I cannot see why. I would really appreciate an extra pair of eyes to look over this.
listView = getListView();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, foodNames);
listView.setAdapter(adapter);
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parentView, View childView, int position, long id) {
TextView tv = (TextView) listView.getSelectedItem();
String value = tv.getText().toString();
Log.v("DEBUG","Name of item clicked" + value);
Intent intent = new Intent(childView.getContext(), FoodItemActivity.class);
intent.putExtra("FoodName", value );
startActivity(intent);
}
public void onNothingSelected(AdapterView parentView) {
}
});
I think you are missing line
startActivity(intent);
:-)
try it this way:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(view.getContext(), FoodItemActivity.class);
intent.putExtra("FoodName", value );
startActivity(intent);
}
});
this will solve your question, and if still problem arises, then do confirm that all the child controls are focusable=false and clickable=false.
I have been struggling using some tutorial for passing a listview option to a new activity and make it the title (I will do other stuff with it later). I have set up a OnClickListener by what is best to put inside it
ListView listView1 = (ListView) findViewById(R.id.sportslist);
String[] items = { "Archery", "Badminton", "Cricket", "Dodgeball", "Equestrian", "Football", "Golf", "Handball", "Ice Hockey", "Ju Jitsu", "Karate", "Lacrosse", "Mountain Biking", "Netball" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
?????
}
});
Thanks
EDIT:Extra code
final TextView changetitle = (TextView) findViewById(R.id.detailedsocietyname);
changetitle.setText(name);
For of all you will need to get the item that was selected:
final String selected = items[position];
Or as doctoror drive has suggested
final String selected = (String) parent.getSelectedItem();
Then you will need to pass that string as an extra to your new activity
Intent i = new Intent(getApplicationContext(), MyClass.class);
i.putExtra("name", selected);
startActivity (i);
And then finally in your next activity
Intent in = getIntent();
String name = in.getStringExtra(("name"));//gets name from intent
public void onItemClick(AdapterView parent, View view,int position, long id)
String str = items[position];
Intent in = new Intent(getApplicationContext(), NextClass.class);
in.putExtra("itemkey", str);
startActivity (in);
}
in public void onItemClick(AdapterView<?> parent, View view,int position, long id) add this code.
Intent i = new Intent(getApplicationContext(), NextClass.class);
i.putExtra("selectedItem", items[position]);
startActivity (i);
for getting value in NextClass Activitiy :
String SelectedItem = getIntent().getStringExtra("selectedItem");
Add the following to you Activity where you have listview
following is the variable that will contain the value that you want to pass to other activity
Declare it before Oncreate statement
// Activity_1
public final static String send_to_other_activity="ListViewSelected_ID";
Add the following code to the listView1.setOnItemClickListener
Intent i= new Intent(Recipe_List.this,Recipe_View.class);
i.putExtra(send_to_other_activity, string.valueof(position));
// itz (key-value) pair on the left key thru which u will access it on other place. on the right value that you want to pass
// Iam passing posiion to other activity here
startActivity(i);
Now on other activity extract this value from the key by adding following statement to the oonCreate of other activity
//Activity_2
getdata_from_list =getIntent().getStringExtra(Activity_1.send_to_other_activity);
now you have teh desired value in getdata_from_list
Hi i have a problem i would like on my android app that uses a list once an item is clicked on the list the id is captured and a query run the data received is then placed in an intent and a new activity is started and the data placed in a another list any help how this can be achieved with an example if possible
yourlist.OnItemClickListener(listenerOflistView);
private OnItemClickListener listenerOflistView = new OnItemClickListener() {
private String getSelectedItemOfList;
public void onItemClick(AdapterView<?> view, View view1, int pos, long arg3) {
Intent in = new Intent(Context, nextActivity.class);
String getSelectedItemOfList = mylist.get(pos).toString();
in.putExtras("AnythingName", getSelectedItemOfList);
startActivity(in);
}
}
In another activity:
Intent in = getIntent();
in.getStringextras("AnythingName");
then, you get the value of another activity.
I think you can achieve by using OnItemClickListener() for ListView. So that when you click list item you will get the content of Listview.
yourlist.OnItemClickListener(listenerOflistView);
private OnItemClickListener listenerOflistView = new OnItemClickListener() {
private String getSelectedItemOfList;
public void onItemClick(AdapterView<?> view, View view1, int pos, long arg3) {
getSelectedItemOfList = mylist.get(pos).toString();
// here you will get selected item. And you can perform rest of your task here.
}
}