tags:

views:

43

answers:

1

I populate ListView with data from a Sqlite database using custom CursorAdapter.

When user moves away from my activity and comes back later I loose the position in my cursor and the ListView is positioned to the first item again.

When skipping startManagingCursor() it works as expected. How do I work around this?

Here is a code snippet called OnCreate():

cursor  = db.getAll();      
startManagingCursor(cursor);
myAdapter = new MyAdapter(listview.getContext(), cursor, true);
listview.setAdapter(myAdapter);
+1  A: 

LatinSuD is correct, you'll want to store the position in the bundle. The managed cursor is requeried when the activity becomes active again so you'll lose your spot. You can look at the notepad3 tutorial for an example of saving state information. http://developer.android.com/resources/tutorials/notepad/notepad-ex3.html

Mike
I've tried that, but cursorFav.moveToPosition(savedposition); doesn't change a ListView position.
Worldscope
Try listview.setSelection(savedposition);
Mike
Thanks. Actually I had to remember selected item position in Listview ItemClickListener (listview.getSelectedItemPosition() in touch mode always returns INVALID_POSITION) and then restore it by listview.requestFocusFromTouch(); listview.setSelection(savedposition);
Worldscope