java - SharedPreferences in a edittext of a AlertDialog -


i'm using texttospeech first time , have problem.. created dialog edittext can write something:

alertdialog.builder alert = new alertdialog.builder(this);  alert.settitle("title"); alert.setmessage("message");  // set edittext view user input  final edittext input = new edittext(this); alert.setview(input);  alert.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) {   value = input.gettext().tostring();   // value!   } });  alert.setnegativebutton("cancel", new dialoginterface.onclicklistener() {   public void onclick(dialoginterface dialog, int whichbutton) {     // canceled.   } });  alert.show(); 

i need save in preferences value.

i created one:

private void prefvalue(string tag) {   string name = savename.getstring(tag, null);   sharedpreferences.editor preferenceseditor = savename.edit();   preferenceseditor.putstring("tag", tag);   preferenceseditor.commit(); } 

well.. in oncreate have do? because tried like

input.settext(savename.getstring("tag", default value")); 

where input edittext inside alertdialog. of course doesn't work because doesn't find input variable.

you can save value on click of positive button.

alert.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { value = input.gettext().tostring();   sharedpreferences sp = getsharedpreference("mykey",0);  sharedpreferences.editor preferenceseditor = sp.edit();  preferenceseditor.putstring("tag", value);  preferenceseditor.commit();  } }); 

now retrieve anywhere want.

sharedpreferences sp = getsharedpreference("mykey",0); string data = sp.getstring("tag",""); 

now set data string edittext.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -