Android: get position of item in a listview given its id:

后端 未结 4 468
Happy的楠姐
Happy的楠姐 2021-01-11 19:46

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.

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-11 20:33

    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.

提交回复
热议问题