开发者

Get position of an item within a ListView?

开发者 https://www.devze.com 2023-02-26 05:17 出处:网络
How would one find the position of a specific item within a ListView? (Populated by SimpleCursorAdapter).

How would one find the position of a specific item within a ListView? (Populated by SimpleCursorAdapter).

The reason I ask: The listview is set to singleChoice mode. 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. What I need to learn is how to reselect the item in the activity's onCreate metho开发者_如何转开发d once it's been repopulated.

My code for saving the selected item's 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:

public abstract int getPosition () 

Since: API Level 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. After the last row is returned another call to next() will leave the cursor past the last entry, at a position of count().

Returns the current cursor position.

Update

To get an item's position based on the id used by the adapter:

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. In your case that would be a 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)?

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. This would allow you to not need to reverse-lookup the position from the _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?

0

精彩评论

暂无评论...
验证码 换一张
取 消