Setting Android ListActivity starting position
I'm displaying data coming from SQLite in a ListActivity. The user can click on an list element which sends him to an Activity that updates the DB.
Once the update Activity is finished, the user gets sent back to the original ListActivity.
When the user comes back from the ListActivity I would like the ListView to be positioned to display the data that she just updated (e.g. in the first position of possible).
I tried positioning the Cursor to point to the entry I want displayed; but that doesn't seem to work (see code below for code example).
protected void onResume() {
super.onResume();
mDbCursor = mDbAdapter.getCursor();
long id = getIntent().getLongExtra(FIRST_ID, -1); // Id of updated row.
while (mDbCursor.moveToNext() &开发者_开发知识库& getId(mDbCursor) != id) {
}
.
. calling setListAdapter(SimpleCursorAdapter);
.
}
I do think the cursor is positioned to the correct row after exiting the while loop.
Have you tried using getListView().setSelection(int position)
on your ListActivity? The position
argument starts at 0 for the list, so it's not the same thing as your id
variable, but in your while loop you could count from zero as you iterate through the Cursor and use that as the position.
The cursor initial position is not considered. You have to use listView.setSelection(int position), and get the position according to your id:
for (int position = 0; mDbCursor.moveToNext() && getId(mDbCursor) != id; position++) {}
setListAdapter(SimpleCursorAdapter);
getListView.setSelection(position);
精彩评论