Get position of an item within a ListView?

后端 未结 3 1860
無奈伤痛
無奈伤痛 2021-02-08 20:05

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

3条回答
  •  死守一世寂寞
    2021-02-08 20:42

    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.

提交回复
热议问题