简体   繁体   English

获取ListView中项目的位置?

[英]Get position of an item within a ListView?

How would one find the position of a specific item within a ListView? 如何在ListView中找到特定项目的位置? (Populated by SimpleCursorAdapter). (由SimpleCursorAdapter填充)。

The reason I ask: The listview is set to singleChoice mode. 我问的原因:listview设置为singleChoice模式。 When the user closes and reopens the app, I'd like the user's selection to be remembered. 当用户关闭并重新打开应用程序时,我希望记住用户的选择。

The way I've done it so far is when the user clicks on an item, the ID of the chosen item is saved to preferences. 到目前为止我的方式是当用户点击某个项目时,所选项目的ID会保存到首选项中。 What I need to learn is how to reselect the item in the activity's onCreate method once it's been repopulated. 我需要学习的是如何重新填充活动的onCreate方法中的项目。

My code for saving the selected item's ID: 我保存所选项目ID的代码:

    @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    Cursor c = (Cursor) l.getItemAtPosition(position);
    selectedItem = c.getLong(c.getColumnIndex("_id"));
}

(I've tried googling, but only seem to find how to get the position of the selected item) (我试过谷歌搜索,但似乎只是找到如何获得所选项目的位置)

Thanks! 谢谢!

You should try 你应该试试

//SimpleCursorAdapter adapter;
final int position = adapter.getCursor().getPosition();

API Docs: API文档:

public abstract int getPosition () 

Since: API Level 1 自:API级别1

Returns the current position of the cursor in the row set. 返回行集中光标的当前位置。 The value is zero-based. 该值从零开始。 When the row set is first returned the cursor will be at positon -1, which is before the first row. 首次返回行集时,光标将位于第一行之前的位置-1。 After the last row is returned another call to next() will leave the cursor past the last entry, at a position of count() . 在返回最后一行之后,对next()另一次调用将使光标超过最后一个条目,位于count()的位置。

Returns the current cursor position. 返回当前光标位置。

Update 更新

To get an item's position based on the id used by the adapter: 根据适配器使用的id获取项目的位置:

private int getItemPositionByAdapterId(final long id)
{
    for (int i = 0; i < adapter.getCount(); i++)
    {
        if (adapter.getItemId(i) == id)
            return i;
    }
    return -1;
}

To get an item's position based on the underlying object's properties (member values) 根据基础对象的属性(成员值)获取项目的位置

//here i use `id`, which i assume is a member of a `MyObject` class, 
//and this class is used to represent the data of the items inside your list:
private int getItemPositionByObjectId(final long id)
{
    for (int i = 0; i < adapter.getCount(); i++)
    {
        if (((MyObject)adapter.getItem(i)).getId() == id)
            return i;
    }
    return -1;
}

I do this straightforward in my own app: 我在自己的应用程序中直截了当地做到了这一点:

long lastItem = prefs.getLong(getPreferenceName(), -1);
if (lastItem >= 0) {
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        if (lastItem == cursor.getLong(0)) {
            spinner.setSelection(cursor.getPosition());
            break;
        }
        cursor.moveToNext();
    }
}

Spinner is populated with the cursor's contents, so I just look through them and compare with the selected item id. Spinner会填充光标的内容,因此我只需查看它们并与所选的项目ID进行比较。 In your case that would be a ListView. 在你的情况下,这将是一个ListView。

When you say, "...reselecting the item in the activity's onCreate method...", do you mean that when the user returns to the ListView activity, whatever item was previously chosen, is now currently at the top of the screen (assuming enough items appear in the list below it)? 当您说“...重新选择活动的onCreate方法中的项目......”时,您的意思是当用户返回ListView活动时,无论先前选择了什么项目,现在都位于屏幕顶部(假设下面的列表中出现了足够的项目)?

If so, then from onListItemClick, you should also make an effort to save the value of position , since it tells you the position in the list of the selected item. 如果是这样,那么从onListItemClick开始,您还应该努力保存position值,因为它会告诉您所选项目列表中的位置。 This would allow you to not need to reverse-lookup the position from the _id . 这将允许您不需要从_id反向查找位置。

Or is that for some reason not an option for your purposes? 或者由于某种原因,这不是您的选择吗? Do you really need to instead figure out the position from the _id ? 你真的需要从_id找出位置吗?

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

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