java - Android Shared Preferences using getter/setter methods fail to display -
my android app fails display shared preferences set in different class. missing in code? save them fine don't display in main activity. see default text when run app. have tried can no avail. can't figure out problem is. below code files:
1. mnseditor.java
public class mnseditor extends activity { private msitem mnses; private button savebtn; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.mns_editor); intent intent = this.getintent(); mnses = new msitem(); mnses.setstartkey(intent.getstringextra("startkey")); mnses.setstartdate(intent.getstringextra("startdate")); mnses.setshiftkey(intent.getstringextra("shiftkey")); mnses.setshift(intent.getstringextra("shift")); mnses.setbkey(intent.getstringextra("bkey")); mnses.setb(intent.getstringextra("b")); mnses.setgkey(intent.getstringextra("gkey")); mnses.setg(intent.getstringextra("g")); mnses.setnxtkey(intent.getstringextra("nxtkey")); mnses.setnxt(intent.getstringextra("nxt")); edittext ms = (edittext) findviewbyid(r.id.sdate); ms.settext(mnses.getstartdate()); edittext sh = (edittext) findviewbyid(r.id.sday); sh.settext(mnses.getshift()); edittext bb = (edittext) findviewbyid(r.id.bb); bb.settext(mnses.getb()); edittext bg = (edittext) findviewbyid(r.id.bg); bg.settext(mnses.getg()); edittext nm = (edittext) findviewbyid(r.id.nxt); nm.settext(mnses.getnxt()); savebtn = (button) findviewbyid(r.id.saveprefs); savebtn.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { saveandfinish(); } }); } private void saveandfinish() { edittext mst = (edittext) findviewbyid(r.id.stday); string mnsesstart = mst.gettext().tostring(); edittext shi = (edittext) findviewbyid(r.id.shiftday); string shift = shi.gettext().tostring(); edittext bb = (edittext) findviewbyid(r.id.bbb); string bbb = b.gettext().tostring(); edittext gg = (edittext) findviewbyid(r.id.ggg); string ggg = gg.gettext().tostring(); edittext nxt = (edittext) findviewbyid(r.id.nxt); string next = nxt.gettext().tostring(); intent intent = new intent(); intent.putextra("startdatekey", mnses.getstartdate()); intent.putextra("startdate", mnsesstart); intent.putextra("shiftkey", mnses.getshift()); intent.putextra("shift", shift); intent.putextra("bkey", mnses.getb()); intent.putextra("b", bbb); intent.putextra("gkey", mnses.getg()); intent.putextra("g", ggg); intent.putextra("nxtkey", mnses.getnxt()); intent.putextra("nxt", next); setresult(result_ok, intent); finish(); } }
2. msdatasource.java
public class msdatasource { msitem mnses; private static final string mns_prefs = "mnskey"; public sharedpreferences mnsprefs; public static final string startdate = "startkey"; public static final string shift = "shiftkey"; public static final string b = "bkey"; public static final string g = "gkey"; public static final string nxt = "nxtkey"; public msdatasource(context context) { mnsprefs = context.getsharedpreferences(mns_prefs, context.mode_private); } public string getthestartdate() { string mnsstart = mensprefs.getstring(startdate, "not seen"); return mnsstart; } public string gettheshift() { string shiftda = mensprefs.getstring(ovulation, "no data"); return shiftda; } public string gettheboy() { string bda = mensprefs.getstring(b, "not accessible"); return bda; } public string getthegirl() { string gda = mensprefs.getstring(g, "does not exist"); return gda; } public string getthenextmenses() { string nxtmns = mensprefs.getstring(nxt, "maybe later"); return nxtmns; } public boolean update(mnsesitem mns) { sharedpreferences.editor editor = mnsprefs.edit(); editor.putstring(mns.getstartkey(), mns.getstartdate()); editor.putstring(mns.getshiftkey(), mns.getshift()); editor.putstring(mns.getbkey(), mns.getb()); editor.putstring(mns.getgkey(), mns.getg()); editor.putstring(mns.getnxtkey(), mns.getnxt()); editor.commit(); return true; } }
3. mst.java
public class mst extends activity { private textview startdate; private textview shiftday; private textview bday; private textview gday; private textview nextday; public static final string startdate = "startkey"; public static final string shift = "shiftkey"; public static final string b = "bkey"; public static final string g = "gkey"; public static final string nxt = "nxtkey"; private static final int editor_activity_request = 1001; private msdatasource datasource; msitem mnses; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.mns_layout); startdate = (textview) findviewbyid(r.id.mnsstartd); shiftday = (textview) findviewbyid(r.id.shd); bday = (textview) findviewbyid(r.id.bbd); gday = (textview) findviewbyid(r.id.bgd); nextday = (textview) findviewbyid(r.id.nxtmnsd); datasource = new msdatasource(this); refreshdisplay(); } private void refreshdisplay() { mnses = datasource.findall(); startdate.settext(datasource.mnsprefs.getstring(startdate, "not retrieved")); shiftday.settext(datasource.mnsprefs.getstring(shift, "unavailable")); bday.settext(datasource.mnsprefs.getstring(b, "not seen")); gday.settext(datasource.mnsprefs.getstring(g, "not found")); nextday.settext(datasource.mnsprefs.getstring(nxt, "no data")); } private void createmns() { msitem mns = msitem.getnew(); intent intent = new intent(this, mnseditor.class); intent.putextra("startkey", mns.getstartkey()); intent.putextra("startdate", mns.getstartdate()); intent.putextra("shiftkey", mns.getshiftkey()); intent.putextra("shift", mns.getshift()); intent.putextra("bkey", mns.getbkey()); intent.putextra("b", mns.getb()); intent.putextra("gkey", mns.getgkey()); intent.putextra("g", mns.getg()); intent.putextra("nxtkey", mns.getnxtkey()); intent.putextra("nxt", mns.getnxt()); startactivityforresult(intent, editor_activity_request); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == editor_activity_request && resultcode == result_ok) { msitem mns = new msitem(); mns.setstartkey(data.getstringextra("startkey")); mns.setstartdate(data.getstringextra("startdate")); mns.setshiftkey(data.getstringextra("shiftkey")); mns.setshift(data.getstringextra("shift")); mns.setbkey(data.getstringextra("bkey")); mns.setb(data.getstringextra("b")); mns.setgkey(data.getstringextra("gkey")); mns.setg(data.getstringextra("g")); mns.setnextmnseskey(data.getstringextra("nxtkey")); mns.setnxt(data.getstringextra("nxt")); datasource.update(mns); refreshdisplay(); } } }
4. msitem.java
@suppresslint("simpledateformat") public class msitem { private string startkey; private string startdate; private string shiftkey; private string shift; private string bkey; private string b; private string gkey; private string g; private string nxtkey; private string nxt; public string getstartkey() { return startkey; } public void setstartkey(string startkey) { this.startkey = startkey; } public string getstartdate() { return startdate; } public void setstartdate(string startdate) { startdate = startdate; } public string getshiftkey() { return shiftkey; } public void setshiftkey(string shiftkey) { this.shiftkey = shiftkey; } public string getshift() { return shift; } public void setshift(string shift) { shift = shift; } public string getbkey() { return bkey; } public void setbkey(string bkey) { this.bkey = bkey; } public string getb() { return b; } public void setb(string b) { b = b; } public string getgkey() { return gkey; } public void setgkey(string gkey) { this.gkey = gkey; } public string getg() { return g; } public void setg(string g) { g = g; } public string getnxtkey() { return nxtkey; } public void setnxtkey(string nxtkey) { this.nxtkey = nxtkey; } public string getnxt() { return nxt; } public void setnxt(string nxt) { nxt = nxt; } public static msitem getnew(){ locale locale = new locale("en_us"); locale.setdefault(locale); string pattern = "yyyy-mm-dd hh:mm:ss z"; simpledateformat formatter = new simpledateformat(pattern); string startdatekey = formatter.format(new date()); string skey = formatter.format(new date()); string bkey = formatter.format(new date()); string gkey = formatter.format(new date()); string nkey = formatter.format(new date()); msitem mnses = new msitem(); mnses.setstartkey(startdatekey); mnses.setstartdate(""); mnses.setshiftkey(skey); mnses.setshift(""); mnses.setbkey(bkey); mnses.setb(""); mnses.setgkey(gkey); mnses.setg(""); mnses.setnxtkey(nkey); mnses.setnxt(""); return mnses; } }
try this:
public string getstartdate(){ mnsprefs = context.getsharedpreferences(startdate, context.mode_private); string mnsstart = mnsprefs.getstring(startdate, ""); return mnsstart; } public string getshift(){ mnsprefs = context.getsharedpreferences(shift, context.mode_private); string shiftda = mnsprefs.getstring(shift, ""); return shiftda; }
its more readable
Comments
Post a Comment