android - Have Multiple Preferences within SharedPreferences for Checkboxes -


how can have separate preference each checkbox saving whether clicked or not? have code works 1 , can repeat make work each 1 feel should able change don't have repeat code.

in oncreate

checkbox checkbox = (checkbox) findviewbyid(r.id.description);     checkbox checkbox1 = (checkbox) findviewbyid(r.id.description1);      sharedpreferences sharedpref = getsharedpreferences("mypref", mode_private);     sharedpreferences sharedpref1 = getsharedpreferences("mypref1", mode_private);     boolean checked = sharedpref.getboolean("checked", false);      boolean checked1 = sharedpref1.getboolean("checked1", false);      checkbox.setchecked(checked);     checkbox1.setchecked(checked1); 

my click handler

public void handlecheckboxclick(view view) {     checkbox tmpchkbox = (checkbox) findviewbyid(view.getid());     if(tmpchkbox.ischecked())     {         tmpchkbox.setbackgroundcolor(color.blue);          sharedpreferences sharedpref = getsharedpreferences("mypref", mode_private);         sharedpref.edit().putboolean("checked", true).commit();      }     else     {         tmpchkbox.setbackgroundcolor(color.red);         sharedpreferences sharedpref = getsharedpreferences("mypref", mode_private);         sharedpref.edit().putboolean("checked", false).commit();     } } 

i got work making click handler , referring see in oncreate make whole separate preference. there way make 1 preference can used on each check box?


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -