I have a listview binded to an arrayadapter of strings... How do i set different clicklistener on each item in the list view
Asked
Active
Viewed 509 times
0
-
Did you try search? There are many topics about this. For example answer on SO http://stackoverflow.com/a/8615535/1228514 – Alex Kucherenko Nov 01 '15 at 20:04
-
i didnt still get what am looking for.. Can you help? – Ayomide Aro Nov 01 '15 at 20:15
2 Answers
0
- Register an item click listener on your ListView with http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener)
- In your AdapterView.OnItemClickListener#onItemClick you will get the view and an id.
You can use the id if you have a given order in your arraylist, or you could use View#findViewById and get the content of the view to figure out which item it is.
That means, you would not set a listener per item, rather you would have one listener for all items, and then do different things based on which item it was.

Erik Živković
- 4,867
- 2
- 35
- 53
-
Ok thanks... But can you gimme an example like a sample code or something? – Ayomide Aro Nov 01 '15 at 20:17
-
@AyomideAro here on StackOverflow you will always get more help if your question is more specific. If you edit your question and fill in your own code we can help you fill in the blanks. – Erik Živković Nov 01 '15 at 20:24
0
I think you could use it like this.
adapter = new ArrayAdapter(getContext(), R.layout.list_item_forecast, R.id.list_item_forecast_textView, new ArrayList<String>());
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//what ever you want to give on item click
}
});
here by checking the position
inside onItemClick
you can assign specific tasks.

Nabeel K
- 5,938
- 11
- 38
- 68