1. Check out our companion app, Forums for Android! Download from Google Play

How to remember listview position and selection

Discussion in 'Application Development' started by kaliki, Mar 8, 2010.

  1. kaliki

    kaliki Member
    Thread Starter
    15

    Jan 21, 2010
    11
    0
    15
    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
     

    Advertisement

  2. Zero

    Zero Member
    16

    Oct 23, 2009
    11
    2
    16
    You need to save the selection position before refreshing your adapter. I use this in onRestart():

    Code (Text):
    1.  
    2.         int selection = mListView.getSelectedItemPosition();
    3.         mAdapter.refresh(); //reload the data, refresh is my own method, you can
    4. //use notifyDataSetChanged()/Invalidated()
    5.         mListView.setSelection(selection);
    6.  
     
  3. kaliki

    kaliki Member
    Thread Starter
    15

    Jan 21, 2010
    11
    0
    15
    Thanks that worked great
     
  4. navsoftdev87

    navsoftdev87 New Member
    5

    Jun 9, 2011
    4
    0
    5
    Should i pass the selected item position to the next screen?
     
  5. alostpacket

    alostpacket Over Macho Grande?
    513

    Nov 29, 2009
    7,978
    3,606
    513
    Android App Developer
    NY
    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
     

Share This Page

Loading...