views:

52

answers:

1

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
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
I can't seem to get this to work. Can you explain in a little more depth?
benjamin schultz
Can you share the code you are using?
Cristian