10

getItemIdAtPosition()是 android 中的一个函数,用于在给定位置的列表视图中获取项目的 id

有没有办法做相反的事情,即获取项目的位置是给定其id的列表视图?

4

4 回答 4

13

不,您必须手动完成。在您使用的适配器内创建一个公共方法;在该方法中,循环适配器项目并检查每个项目 ID。如果是 == 到方法参数,则返回索引。

public int getItemPosition(long id)
{
    for (int position=0; position<mList.size(); position++)
        if (mList.get(position).getId() == id)
            return position;
    return 0;
}

更新:如果查找性能代表您的用例存在问题,您不妨在适配器中为位置/id 保存一个 HashMap。

更新:如果您想在适配器之外使用此方法,请使用以下方法:

private int getAdapterItemPosition(long id)
{
    for (int position=0; position<mListAdapter.getCount(); position++)
        if (mListAdapter.get(position).getId() == id)
            return position;
    return 0;
}
于 2013-08-02T11:42:46.183 回答
4

不确定问题是否仍然存在。尽管如此,我想我会分享我是如何做到的,以便它可以帮助那些想要做同样事情的人。

您可以使用视图的标记功能来存储该视图的 id 与其在列表中的位置之间的映射。

android 开发者网站上的标签文档很好地解释了这一点:

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)

示例:在列表适配器的 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;
}

要检索视图的位置,您可以执行以下操作:

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

需要注意的一点是,您确实需要引用要检索其位置的视图。您如何获得 View 的参考取决于您的具体情况。

于 2014-07-21T07:58:19.190 回答
0

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.

于 2013-08-02T12:16:36.237 回答
0

现在回答为时已晚,但为了利益......
例如,如果您有 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());
于 2016-02-14T20:02:09.980 回答