I have a ListView which its contents could change at any time (eg. a list of cities within a state; a new city could be added to the state in our database at anytime). How do I implement, if possible, the onListItemClick event to work with this? For example, if a user selects a certain city in the ListView, i should be able to pass a value that independently identifies what city was clicked onto my next activity. I can't listen for positions because they could change with an addition or removal of a city. Any suggestions?
+1
A:
In this case you must write your own Adapter (for instance, extending the BaseAdapter
) and overwrite the getItemId
method:
@Override
public long getItemId(int posicion) {
return somethingThatIdentifyTheCityThatWasClicked;
}
The rest will be piece of cake since the onListItemClick
method gets that ID:
@Override
protected void onListItemClick(ListView l, View v, int position, long thisID)
{
// use the 'thisID' variable
}
Cristian
2010-06-25 20:06:06
I'm going to try this out and get back to you, but it looks like it's just what I need. Thanks!
benjamin schultz
2010-06-25 20:20:02
I can't seem to get this to work. Can you explain in a little more depth?
benjamin schultz
2010-06-28 15:50:36
Can you share the code you are using?
Cristian
2010-06-28 16:19:48