• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to remember listview position and selection

kaliki

Newbie
Hi there
I have a problem with a listview i'm using. I load the data in the listview from a cursorAdapter and when i select an item an onItemClickListener launches the corresponging activity. Now when i press the back button i would like
the listview to remember the previous position and selection but it just loads the list from the start, none of these is remembered.Can anyone help me with an example? Thank you
 
You need to save the selection position before refreshing your adapter. I use this in onRestart():

Code:
        int selection = mListView.getSelectedItemPosition();
        mAdapter.refresh(); //reload the data, refresh is my own method, you can 
//use notifyDataSetChanged()/Invalidated()
        mListView.setSelection(selection);
 
If you need it on the next screen :) Otherwise it's better to save in the instance state.


You guys may want to look at Loader too if you are using cursor/simplecursor adapters

It's new in Android 3.0 but they added it to the compatibility package so you can use it all the way back to 1.6

hth
 
Back
Top Bottom