android - save checked state of checkbox in multi select listview -


i'm new multi-select listview. want save checked state of checkbox in listview if user closes app , opens again, selected checkbox still remain selected. there way this. searched , found can done using sharedpreference didn't more information on how use it. thanks

public class mainactivity extends appcompatactivity {      listview mylist;     button getchoice;      string[] listcontent = {              "january",             "february",             "march",             "april",             "may",             "june",             "july",             "august",             "september",             "october",             "november",             "december"      };      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          mylist = (listview)findviewbyid(r.id.list);         getchoice = (button)findviewbyid(r.id.getchoice);          arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_multiple_choice, listcontent);         mylist.setchoicemode(listview.choice_mode_multiple);          mylist.setadapter(adapter);          getchoice.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 string selected = "";                 int cntchoice = mylist.getcount();                  sparsebooleanarray sparsebooleanarray = mylist.getcheckeditempositions();                 for(int = 0; < cntchoice; i++){                     if(sparsebooleanarray.get(i)) {                         selected += mylist.getitematposition(i).tostring() + "\n";                      }                  }                  toast.maketext(mainactivity.this, selected, toast.length_long).show();              }         });        } } 

you can save state, example, in sharedpreferences.

so oncreate , ondestroy methods like:

sharedpreferences sharedpreferences = getsharedpreferences("mysharedprefs", mode_private);  @override protected void oncreate(final bundle savedinstancestate) {     ...     set<string> checkeditemssource = sharedpreferences.getstringset("checked_items", new hashset<string>());     sparsebooleanarray checkeditems = converttocheckeditems(checkeditemssource);     (int = 0; < checkeditems.size(); i++) {         int checkedposition = checkeditems.keyat(i);         listview.setitemchecked(checkedposition, true);     } }  @override protected void ondestroy() {     super.ondestroy();     sparsebooleanarray checkeditems = listview.getcheckeditempositions();     set<string> stringset = converttostringset(checkeditems);     sharedpreferences.edit()             .putstringset("checked_items", stringset)             .apply(); }  private sparsebooleanarray converttocheckeditems(set<string> checkeditems) {     sparsebooleanarray array = new sparsebooleanarray();     for(string itempositionstr : checkeditems) {         int position = integer.parseint(itempositionstr);         array.put(position, true);     }      return array; }  private set<string> converttostringset(sparsebooleanarray checkeditems) {     set<string> result = new hashset<>();     (int = 0; < checkeditems.size(); i++) {         result.add(string.valueof(checkeditems.keyat(i)));     }      return result; } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -