Store value in SharedPreference and get From it in android -
i new android , have been using code save value of checkboxes when app closes works fine (in settings.class)
public void putbooleaninpreferences(boolean ischecked,string key){ sharedpreferences sharedpreferences = this.getpreferences(activity.mode_private); sharedpreferences.editor editor = sharedpreferences.edit(); editor.putboolean(key, ischecked); editor.commit(); } public boolean getbooleanfrompreferences(string key){ sharedpreferences sharedpreferences = this.getpreferences(activity.mode_private); boolean ischecked = sharedpreferences.getboolean(key, false); return ischecked; }
but want use same saved value in activity (progress.class)
here activities
settings.class
checkbox_one = (checkbox) findviewbyid(r.id.checkbox1); boolean ischecked = getbooleanfrompreferences("ischecked"); log.i("start",""+ischecked); checkbox_one.setchecked(ischecked); //checkbox_one.setchecked(true);//enable default checkbox_one.setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton view, boolean ischecked) { log.i("boolean",""+ischecked); settings.this.putbooleaninpreferences(ischecked,"ischecked"); } }); checkbox_two = (checkbox) findviewbyid(r.id.checkbox2); boolean ischeckedtwo = getbooleanfrompreferences("ischeckedtwo"); checkbox_two.setchecked(ischeckedtwo ); //checkbox_two.setchecked(true);//enable default checkbox_two.setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton view, boolean ischecked) { settings.this.putbooleaninpreferences(ischecked,"ischeckedtwo"); } }); checkbox_three = (checkbox) findviewbyid(r.id.checkbox3); boolean ischeckedthree = getbooleanfrompreferences("ischeckedthree"); checkbox_three.setchecked(ischeckedthree ); checkbox_three.setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton view, boolean ischecked) { settings.this.putbooleaninpreferences(ischecked,"ischeckedthree"); } }); checkbox_four = (checkbox) findviewbyid(r.id.checkbox4); boolean ischeckedfour = getbooleanfrompreferences("ischeckedfour"); checkbox_four.setchecked(ischeckedfour ); //checkbox_four.setchecked(true);//enable default checkbox_four.setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton view, boolean ischecked) { settings.this.putbooleaninpreferences(ischecked,"ischeckedfour"); } }); checkbox_five = (checkbox) findviewbyid(r.id.checkbox5); boolean ischeckedfive = getbooleanfrompreferences("ischeckedfive"); checkbox_five.setchecked(ischeckedfive ); checkbox_five.setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton view, boolean ischecked) { settings.this.putbooleaninpreferences(ischecked,"ischeckedfive"); } }); } public void putbooleaninpreferences(boolean ischecked,string key){ sharedpreferences sharedpreferences = this.getpreferences(activity.mode_private); sharedpreferences.editor editor = sharedpreferences.edit(); editor.putboolean(key, ischecked); editor.commit(); } public boolean getbooleanfrompreferences(string key){ sharedpreferences sharedpreferences = this.getpreferences(activity.mode_private); boolean ischecked = sharedpreferences.getboolean(key, false); return ischecked; } //-------------------------//
and here progress.class
progressbar1 = (progressbar) findviewbyid(r.id.progressbar1); progressbar2 = (progressbar) findviewbyid(r.id.progressbar2); checkbox1 = (checkbox) findviewbyid(r.id.checkbox1); checkbox2 = (checkbox) findviewbyid(r.id.checkbox2); checkbox1.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { updateprogressbars(); } }); checkbox2.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { updateprogressbars(); } }); } public void updateprogressbars() { progressbar1.setvisibility(view.gone); progressbar2.setvisibility(view.gone); if (checkbox1.ischecked() && checkbox2.ischecked()) { progressbar2.setvisibility(view.visible); } else if (checkbox1.ischecked()) { progressbar1.setvisibility(view.visible); }
i have implemented same requirement in application. followed below process.
since created sharedpreferences, in target activity use below.
sharedpreferences setprefsd=preferencemanager.getdefaultsharedpreferences(getapplicationcontext());
now boolean show below:
setprefsd.getboolean(key,value) //key 1 have used while declaring
let me know how goes
edit--------------------------------------------------------------------------------
change key key should string
value , make ischecked
static access activity
to put value in shared preferences
editor.getboolean("key", ischecked);
to retrive value
setprefsd.getboolean("key", ischecked)
edit2------------------------------------------------------ change share preferences insertion below.
public void putbooleaninpreferences(boolean ischecked,string key){ sharedpreferences sharedpreferences =preferencemanager.getdefaultsharedpreferences(getapplicationcontext()); sharedpreferences.editor editor = sharedpreferences.edit(); editor.putboolean("key", ischecked); editor.commit(); }
Comments
Post a Comment