android - How to remember selected values in Multiple choice Alert Dialog? -


i using alertdialog multiple choice show list of check-able items.

when user selects of values, can theirs index , save list. working fine.

but want when user again open alertdialog have selected/checked values selected before.

here code:

 alertdialog.builder builder = new alertdialog.builder(getactivity());     builder.setmultichoiceitems(r.array.array_cousine, null,             new dialoginterface.onmultichoiceclicklistener() {                  @override                 public void onclick(dialoginterface dialog, int indexselected,                                     boolean ischecked) {                     if (ischecked) {                         seleteditems.add(++indexselected);                     } else if (seleteditems.contains(indexselected)) {                            seleteditems.remove(integer.valueof(++indexselected));                     }                 }             })             // set action buttons             .setpositivebutton("ok", new dialoginterface.onclicklistener() {                 @override                 public void onclick(dialoginterface dialog, int id) {                       string[] explist = getresources().getstringarray(r.array.array_cousine);                      (int = 0; < seleteditems.size(); i++) {                         int selected = seleteditems.get(i);                         string selectedstring = explist[selected - 1];                         selecteditemsname.add(selectedstring);                     }                      stringbuilder stringbuilder = new stringbuilder();                      (int j = 0; j < selecteditemsname.size(); j++) {                         string text = selecteditemsname.get(j);                          stringbuilder = stringbuilder.append(" "+text);                      }                      log.d("tag", "string builder: " + stringbuilder);                     tvcusine.settext(stringbuilder);                      dialog.dismiss();                  }             })             .setnegativebutton("cancel", new dialoginterface.onclicklistener() {                 @override                 public void onclick(dialoginterface dialog, int id) {                     dialog.dismiss();                 }             });      dialog dialog = builder.create();//alertdialog dialog;     dialog.show(); 

here picture:

enter image description here

the second parameter in builder.setmultichoiceitems boolean[] passing in null. show items checked when opens pass in array true in position of each item want checked. these values can set after array created using boolean[position] = value


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -