android - How to use SharedPreferences to store int that is incremented everytime the user clicks a button & retrieve value next time the app is launched -
im beginner android dev, , ive been working on pretty straightforward voting app school.
basically i've created objects in multiple activities act int counters record votes(in form of clicks) various posts. have achieved , able view results counters in different activity. also, activity shifts new activity next position in voting system automatically.
i realised lose data/votes objects(counters) if app killed or crashed. therefore, tried implementing shared preferences store , update count of votes when button clicked.
however, app crashes when use code reassign shared preference variable counter objects value.
i cannot seem understand , fix code counter resumes last value. below code:
public class spl extends appcompatactivity { sharedpreferences retrieve = getapplicationcontext().getsharedpreferences("result", mode_private); public int spl1count = retrieve.getint("hb1", 0); public int spl2count = retrieve.getint("hb2", 0); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_spl); final button spl1vb = (button) findviewbyid(r.id.spl1vb); final button spl2vb = (button) findviewbyid(r.id.spl2vb); spl1vb.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { spl1count ++; sharedpreferences sharedpref = getsharedpreferences("result", context.mode_private); sharedpreferences.editor editor = sharedpref.edit(); editor.putint("hb1", spl1count); editor.apply(); toast.maketext(getapplicationcontext(),"saved ", toast.length_short).show(); intent intent = new intent(getapplicationcontext(), aspl.class); intent.setflags(intent.flag_activity_clear_top); startactivity(intent); finish(); } }); spl2vb.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { spl2count ++; sharedpreferences sharedpref = getsharedpreferences("result", context.mode_private); sharedpreferences.editor editor = sharedpref.edit(); editor.putint("hb2", spl2count); editor.apply(); toast.maketext(getapplicationcontext(),"saved ", toast.length_short).show(); intent intent = new intent(getapplicationcontext(), aspl.class); intent.setflags(intent.flag_activity_clear_top); startactivity(intent); finish(); } }); }
the app crashes , after looking @ log tells me there error on line used shared preference 'retrieved' old data fit counter.
if remove this, counter sets 0 when launched again, believe because counter isn't being set shared preference value.
here code collecting results shared preference in activity:
sharedpreferences sharedpref = getsharedpreferences("result", context.mode_private); final int spl1r = sharedpref.getint("hb1", 0); final int spl2r = sharedpref.getint("hb2", 0);
then use alert dialog box display spl1r , spl2r.
i've spent hours on internet searching solution none have worked me. appreciated. in advance
so updated code based on evin's answer, code still crashes, time in oncreate
here new code:
public class spl extends appcompatactivity { sharedpreferences retrieve; public int spl1count; public int spl2count; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); retrieve = getapplicationcontext().getsharedpreferences("result", mode_private); spl1count = retrieve.getint("hb1", 0); spl2count = retrieve.getint("hb2", 0); button spl1vb = (button) findviewbyid(r.id.spl1vb); button spl2vb = (button) findviewbyid(r.id.spl2vb); spl1vb.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { spl1count ++; sharedpreferences sharedpref = getsharedpreferences("result", context.mode_private); sharedpreferences.editor editor = sharedpref.edit(); editor.putint("hb1", spl1count); editor.apply(); toast.maketext(getapplicationcontext(),"saved ", toast.length_short).show(); intent intent = new intent(getapplicationcontext(), aspl.class); intent.setflags(intent.flag_activity_clear_top); startactivity(intent); finish(); } }); spl2vb.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { spl2count ++; sharedpreferences sharedpref = getsharedpreferences("result", context.mode_private); sharedpreferences.editor editor = sharedpref.edit(); editor.putint("hb2", spl2count); editor.apply(); toast.maketext(getapplicationcontext(),"saved ", toast.length_short).show(); intent intent = new intent(getapplicationcontext(), aspl.class); intent.setflags(intent.flag_activity_clear_top); startactivity(intent); finish(); } }); }
am still doing wrong? thank you.
put these lines inside oncreate
:
sharedpreferences retrieve = getapplicationcontext().getsharedpreferences("result", mode_private); public int spl1count = retrieve.getint("hb1", 0); public int spl2count = retrieve.getint("hb2", 0);
you initializing them when have no reference android context yet (which built in oncreate
method of activity/application) that's why getsharedpreferences method of applicationcontext null reference.
if still want declare these values member variables, class (splitting declaration , assignment):
public class spl extends appcompatactivity { sharedpreferences retrieve; public int spl1count; public int spl2count; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_spl); retrieve = getapplicationcontext().getsharedpreferences("result", mode_private); spl1count = retrieve.getint("hb1", 0); spl2count = retrieve.getint("hb2", 0); final button spl1vb = (button) findviewbyid(r.id.btn1); final button spl2vb = (button) findviewbyid(r.id.btn2); /* listeners */ } }
Comments
Post a Comment