getItemIdAtPosition()
is a function in android used to get the id of an item in a list view given its position
is there any way of doing the reverse, i.
No. Depending on what adapter you're using to back your ListView
the id and position may be the same (I haven't looked at all BaseAdapter
subclasses so I cannot say for sure). If you look at the source code for ArrayAdapter you will see that getItemId
actually returns the position of the object in the adapter. If the position and id are the same, there is no need to use one to get the other. Otherwise you just need to search the adapter for the object your looking for - using either position or id - and you can find the other value.
If what you're talking about is getting objects using some unique key - i.e. one that you define - that can be done. What you need to do is set up your adapter to take a HashMap
instead of an ArrayList
or regular List
of objects. Then you can create your own method to find by key by simply pulling that value from the HashMap
.