简体   繁体   中英

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

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, ie getting the position of an item is a list view given its id?

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<mList.size(); position++)
        if (mList.get(position).getId() == id)
            return position;
    return 0;
}

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<mListAdapter.getCount(); position++)
        if (mListAdapter.get(position).getId() == id)
            return position;
    return 0;
}

Not sure if the question is still open. Nevertheless, thought I will share how I did it so that it may help someone who is looking to do the same thing.

You can use the tag feature of a view to store the mapping between that view's id and its position in the list.

The documentation for tags on the android developer site explains it well:

http://developer.android.com/reference/android/view/View.html#Tags

http://developer.android.com/reference/android/view/View.html#setTag(int, java.lang.Object)

http://developer.android.com/reference/android/view/View.html#getTag(int)

Example: In the getView method of your list adapter, you can set the mapping for that view's id and its position in the list, something like:

public View getView (int position, View convertView, ViewGroup parent)
{
     if(convertView == null)
     {
          // create your view by inflating a xml layout resource or instantiating a
          // custom view class
     }
     else
     {
          // Do whatever you want with the convertView object like
          // finding a child view that you want to set some property of,etc.  
     }

     // Here you set the position of the view in the list as its tag
     convertView.setTag(convertView.getId(),position);

     return convertView;
}

To retrieve the position of the view, you would do something like:

int getItemPosition(View view)
{
    return view.getTag(view.getId());
}

A point to be noted is that you do need to have a reference to the View whose position you want to retrieve. How you get hold of the View's reference is dependent on your specific case.

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 - ie 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 .

it's too late to answer but for benefit...
for example if you have listview and you want to get id with click listener you can get it by >>

cListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {


          @Override
                    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
              // for id or any informations

                                itemID = String.valueOf(catList.get(position).getItemID());

// for name or any informations

                                    itemName = String.valueOf(catList.get(position).getItemName());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM