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

后端 未结 4 440
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:32

    No. You have to do it manually. Create a public method inside the adapter you are using; in that method, loop on the adapter items and check each item id. If it is == to the method param, then return the index.

    public int getItemPosition(long id)
    {
        for (int position=0; position

    Update: You might as well save a HashMap for position/id in your adapter, if lookup performance represents a problem for your use case.

    Update: If you want to use this method outside the adapter, then use below:

    private int getAdapterItemPosition(long id)
    {
        for (int position=0; position

提交回复
热议问题