简体   繁体   English

Android:在列表视图中获取项目的位置给定其ID:

[英]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 getItemIdAtPosition()是android中的一个函数,用于获取列表视图中项目的id给定其位置

is there any way of doing the reverse, ie getting the position of an item is a list view given its id? 有没有办法做反过来,即获取项目的位置是一个列表视图给定其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. 在该方法中,循环适配器项并检查每个项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. 更新:如果查找性能代表您的用例问题,您也可以在适配器中保存位置/ ID的HashMap。

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. 您可以使用视图的标记功能来存储该视图的id与其在列表中的位置之间的映射。

The documentation for tags on the android developer site explains it well: android开发者网站上的标签文档很好地解释了它:

http://developer.android.com/reference/android/view/View.html#Tags 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#setTag(int,java.lang.Object)

http://developer.android.com/reference/android/view/View.html#getTag(int) 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: 示例:在列表适配器的getView方法中,您可以设置该视图的id及其在列表中的位置的映射,例如:

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. 如何掌握View的参考取决于您的具体情况。

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). 不会。根据您用来支持ListView的适配器,id和位置可能是相同的(我没有查看所有BaseAdapter子类,所以我不能肯定地说)。 If you look at the source code for ArrayAdapter you will see that getItemId actually returns the position of the object in the adapter. 如果查看ArrayAdapter的源代码,您会看到getItemId实际上返回了适配器中对象的位置。 If the position and id are the same, there is no need to use one to get the other. 如果position和id相同,则不需要使用一个来获取另一个。 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. 否则,您只需要在适配器中搜索您要查找的对象 - 使用位置或ID - 您就可以找到其他值。

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. 您需要做的是设置适配器以获取HashMap而不是ArrayList或常规的对象List Then you can create your own method to find by key by simply pulling that value from the HashMap . 然后,您可以通过简单地从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 >> 例如,如果你有listview,并且你想获得点击监听器的id,你可以通过>>获得它

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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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