java - Android - How to save checkbox state (checked/unchecked) after changing activity -
i have searched through stackoverflow, have not found proper answer yet.
i have created listview
(iteration of checkbox
+ itemview
) , populated through customadapter (which extends baseadapter).
i have button takes values , print on screen via toast.
so far, good.
next step, still have button in mainactivity
, listview
in child activity reach clicking image (imageview
placed in mainactivity
). can still check checkboxes, face 2 issues:
- i still not able pass values
mainactivity
, printed on screen (or manipulated) - as press button go
mainactivity
, press again image, everycheckbox
checked not checked anymore (they came default state)
i don't think code needed, comes standard implementation (listview
- customadapter
viewholder
implementation, ...), in case let me know.
thanks lot in advance!
you can put checkboxes checked sharedpreferences. move listview initialization code activity's onresume
method.
sample class handle sharedpreferences
data:
class datahandler { private final sharedpreferences datastore; datahandler(context mcontext) { datastore = mcontext.getsharedpreferences("appname", context.mode_private); } int which() { return datastore.getint("some_key",0); } void setcheckeditem(int itemwhat) { datastore.edit().putint("some_key",itemwhat).apply(); } }
for multiple values, can put them array convert them string using tostring()
method , save. and, values:
string x = "2,3,4,5"; //assume string[] y = new string[]{x}; int checkablepositions = integer.parseint(y[0]); // y[0]....y[y.length-1]
now, @ mainactivity's onresume()
, assume have initialized listview 'mainlist'.
checkbox x1y2z3 = (checkbox)mainlist.getchildat(new datahandler(getbasecontext).which()); x1y2z3.setchecked(true);
and saving item,
i recommend show them in alert-dialog instead of in toast. set positive button values below code , save them.
or, if directly save values listview onclick
:
mainlist.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view view, int position, long id) { new datahandler(getbasecontext()).setcheckeditem(position); } });
that's it. i'm new @ programming (as can see stackoverflow rep) hope able you.
the main concept : store value → value → parse value → show on ui.
Comments
Post a Comment