开发者

Using onListItemClick with data driven ListView

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?


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜