android - Keep listview item selected even if view changes -


i have listview in fragment. when select listview item, gets highlighted , open fragment.

now, want is, when move previous fragment, list item should stay selected.

how can that?

i have implemented like

add 2 methods in adapter

 private int selectedindex = listview.no_id;   public int getselectedindex() {     return selectedindex; }  public void setselectedindex(int index) {     this.selectedindex = index;      // re-draw list informing view of changes     notifydatasetchanged(); } 

and in adapter getview(...)

  // highlight selected item in list     if (selectedindex != -1 && selectedindex == position) {             yourview.setbackgroundresource(r.color.lightred);      } 

and implement in fragment in setonitemclicklistener onitemclick(...) like

adapter.setselectedindex(position); 

save selected value in preferences , when come again call in fragment on resume(...)

adapter.setselectedindex(selectedindex); 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -