Question

I'm wondering a simple way to find back the object corresponding to the clicked item..

They're tons of examples on the web on how to figure out the ListView setup with the setListAdpater, but much less on how to well handle its listener.

Is "by position" the only way ?? I'm wondering a possibility to associate the objects itselves to the adapter, to not have to use their position in list (or even the displayed String!) to find back the Object referred by the clicked label..

Was it helpful?

Solution

Position is what is used always. The ListView works with using the position.

But if you want to access the ListAdapter and get a value out by providing a string, than you will have to extend ListView and implement that functionality yourself. You can overwrite the different methods that handle adding and removing and keep a HashMap where you keep the string representing the object. Then through a getObject(String key) you return the object that is in the hashmap for that key.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top