android - Can not change the text using SharedPreferences -


i made small project try change name edit text using share preference.here code

sharepreferenceownactivity.java

public class sharepreferenceownactivity extends activity {       button b1;     textview t1;       /** called when activity first created. */@override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.main);           b1 = (button) findviewbyid(r.id.b1);         t1 = (textview) findviewbyid(r.id.t1);         loadpreferences();             b1.setonclicklistener(new onclicklistener() {              public void onclick(view v) {                  toast.maketext(getapplicationcontext(), "settings", toast.length_short).show();                 intent intent = new intent().setclass(sharepreferenceownactivity.this, sharepreferenceactivity.class);                 startactivity(intent);              }         });      }     private void loadpreferences() {         sharedpreferences sharedpreferences = getpreferences(mode_private);         string strsavedmem1 = sharedpreferences.getstring("mem1", "");         t1.settext(strsavedmem1);      } } 

sharepreferenceactivity.java

public class sharepreferenceactivity extends activity {       edittext e1;     button b2;     /** called when activity first created. */@override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.settings);           e1 = (edittext) findviewbyid(r.id.e1);         b2 = (button) findviewbyid(r.id.b2);           b2.setonclicklistener(new onclicklistener() {              public void onclick(view v) {                  savepreferences("mem1", e1.gettext().tostring());                 intent intent = new intent().setclass(sharepreferenceactivity.this, sharepreferenceownactivity.class);                 startactivity(intent);              }         });       }      private void savepreferences(string key, string value) {         sharedpreferences sharedpreferences = getpreferences(mode_private);         sharedpreferences.editor editor = sharedpreferences.edit();         editor.putstring(key, value);         editor.commit();     } } 

here edit text doesnot change textview. :( why? did not error

you have problem in following statement.

sharedpreferences sharedpreferences = getpreferences(mode_private); 

as per google doc

the above retrieves sharedpreferences object accessing preferences private activity

so error have done is

you setting preference in preference object private sharepreferenceactivity

and

you trying read preference object private sharepreferenceownactivity

so solution is

replace

sharedpreferences sharedpreferences = getpreferences(mode_private); 

by

sharedpreferences sharedpreferences = getsharedpreferences("mypref",                                       context.mode_private); 

in both activities.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -