android - Persist AlertDialog checked options -



android - Persist AlertDialog checked options -

i've been struggling more 2 weeks. been on questions sharedpreferenes , other 'hacks' persist multiple selection alertdialog. unfortunately still can't create work.

can please explain me how create thing work ? multiple selection alertdialog works. still can't save selected items..

my code :

public class timelinesettings extends dialogfragment { context context; final arraylist selected_categories = new arraylist(); final string[]items = {"fourniture","nourriture","voyages","habillement","médias","autres"}; tinydb tinydb = new tinydb(context); private sharedpreferences sharedpreference; private sharedpreferences.editor sharedprefeditor; @override public dialog oncreatedialog(bundle savedinstancestate) { alertdialog.builder builder = new alertdialog.builder(getactivity()); // set dialog title builder.settitle("choisissez vos paramètres") .setmultichoiceitems(items, null, new dialoginterface.onmultichoiceclicklistener() { @override public void onclick(dialoginterface dialog, int indexselected, boolean ischecked) { if (ischecked) { // if user checked item, add together selected items selected_categories.add(indexselected); } else if (selected_categories.contains(indexselected)) { // else, if item in array, remove selected_categories.remove(integer.valueof(indexselected)); } } }) // set action buttons .setpositivebutton("ok", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int id) { tinydb.putlist("selected",selected_categories); } }) .setnegativebutton("annuler", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int id) { } }); homecoming builder.create(); } }

thanks help.

ps : came across answer, if can explain me how create work, great.

be careful because saving integers in preferences , converting arraylist of integers arraylist of strings. may cause exception. utilize arraylists of strings everywhere instead.

you missing part where, before show dialogfragment, have read saved choices , check them in dialogfragment.

there easiest ways of doing want let's take approach:

as have noticed, setmultichoiceitems() method receives string[] items of dialogfragment , boolean [] define if checked or not. going utilize array persist our choices. array must have same length items array , set false.

on first time launch dialogfragment, no items checked. on sec time, when have items checked, going read choices tinydb , populate boolean array statement. feed dialogfragment builder.

the items saved appear checked on dialogfragment, wished do.

here finish changed , working code:

public class timelinesettings extends dialogfragment { arraylist<string> selected_categories = new arraylist<string>(); final string[]items = {"fourniture","nourriture","voyages","habillement","médias","autres"}; boolean[] itemschecked = {false, false, false, false, false, false}; tinydb tinydb; private sharedpreferences sharedpreference; private sharedpreferences.editor sharedprefeditor; @override public dialog oncreatedialog(bundle savedinstancestate) { alertdialog.builder builder = new alertdialog.builder(getactivity()); tinydb = new tinydb(getactivity()); selected_categories = tinydb.getlist("selected"); for(int i=0;i<itemschecked.length;i++){ if(selected_categories.contains((string)string.valueof(i))) itemschecked[i]=true; } // set dialog title builder.settitle("choisissez vos paramètres") .setmultichoiceitems(items, itemschecked, new dialoginterface.onmultichoiceclicklistener() { @override public void onclick(dialoginterface dialog, int indexselected, boolean ischecked) { if (ischecked) { // if user checked item, add together selected items if(!selected_categories.contains((string)string.valueof(indexselected))){ selected_categories.add(string.valueof(indexselected)); itemschecked[indexselected]=true; } } else if (selected_categories.contains((string)string.valueof(indexselected))) { // else, if item in array, remove selected_categories.remove((string)string.valueof(indexselected)); itemschecked[indexselected]=false; } } }) // set action buttons .setpositivebutton("ok", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int id) { tinydb.putlist("selected",selected_categories); } }) .setnegativebutton("annuler", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int id) { } }); homecoming builder.create(); } }

android arraylist sharedpreferences alertdialog

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -