views:

18

answers:

1

Hello.

I'm developing an Android application.

How can I setup the row id for every item on a ListView? I don't want to use SimpleCursorAdapter. I have my owns objects, and I want to use them.

These objects have and ID and a NAME. The name will be displayed and the ID will be stored to retrieve it on onListItemClick(ListView parent, View v, int position, long id).

Any advice?

Thanks.

A: 

One simple solution would be to create a hash that maps names to ids:

Map<String, Integer> map = new HashMap<String, Integer>();
for(MyObject obj: objectsList) {
  map.put(obj.getName(), Integer.valueOf(obj.getId()));
}



String name = getListView().getItemAtPosition((int) id).toString();
int id = map.get(name).intValue();
kgiannakakis