java - Load strings from sharedpreferences in EditText (two classes) -
i have 2 different activity's. first 1 showed user first time - store phonenumber inside sharedprefs. think problem load prefs method in case leave here make more relevant future users. looks in both activitys "mobilnummer" saved - having problem display them.
the idea overwritten if user updates - think method that?
onstartup.java
public static final string prefs_name = "mobilenumbersaved"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mobilnummer = (edittext) findviewbyid(r.id.mobilnummer); final button button = (button) findviewbyid(r.id.ntonboarding);} public void savetomobile(view v) { if (mobilnummer.gettext().tostring().length() >= 8) { fragmenteduser.savemobile(mobilnummer.gettext().tostring()); // saves parse.com (this works) sharedpreferences settings = getsharedpreferences(prefs_name, 0); sharedpreferences.editor editor = settings.edit(); editor.putstring("mobilnummer", mobilnummer.tostring()); editor.commit(); } }
profile
edittext mobilnummer; public static final string prefs_name = "mobilenumbersaved"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mobilnummer = (edittext) findviewbyid(r.id.mobilnummer);
this issue/problem think! (code below)
sharedpreferences mobilenumbersaved = this.getsharedpreferences("mobilnummer", context.mode_private); string tempmobilnummer = mobilnummer.gettext().tostring(); mobilnummer.settext(tempmobilnummer); // if user wants change mobilenumber mobilnummer.setoneditoractionlistener(new textview.oneditoractionlistener() { @override public boolean oneditoraction(textview v, int actionid, keyevent event) { boolean handled = false; if (actionid == editorinfo.ime_action_send) { if (mobilnummer.gettext().tostring().length() >= 8) fragmenteduser.savemobile(mobilnummer.gettext().tostring()); sharedpreferences settings = getsharedpreferences(prefs_name, 0); sharedpreferences.editor editor = settings.edit(); editor.putstring("mobilnummer", mobilnummer.tostring()); editor.commit(); tlfstatus.setimageresource(r.drawable.ok); handled = true; } return handled; } });
i think here error:
sharedpreferences mobilenumbersaved = this.getsharedpreferences("mobilnummer", context.mode_private); //here should mobilenumbersaved instead of mobilnummer, because tehre no sense set same text textview again after got there string tempmobilnummer = mobilenumbersaved.gettext().tostring(); mobilnummer.settext(tempmobilnummer);
but correct way value shared preferences this:
sharedpreferences prefs = getsharedpreferences(prefs_name, mode_private); string savedmobilenumber = prefs.getstring("mobilnummer", ""); mobilnummer.settext(savedmobilenumber);
update: when save value in shared preferences, save edittext, not it's value, when saving:
sharedpreferences.editor editor = settings.edit(); //mobilnumber edittext, it's text editor.putstring("mobilnummer", mobilnummer.gettext().tostring()); editor.commit();
and else still same wrote above
Comments
Post a Comment