java - How can I make my android app have SharedPreferences as just one set of preferences, so that I can control it and clear the preferences with a button -


i trying have sharedpreferences once , control different methods rather having different sharedpreferences in different methods. have sharedpreferences in oncreate, loadpreferences, savepreferences , cleartextviews methods. want make have preferences saved textviews entered text in edittext , able clear them button. please me if can. here relevant code:

public class notesactivity extends activity implements onclicklistener {

button savenote; button cleartext; edittext note; textview textsavednote1, textsavednote2, textsavednote3, textsavednote4, textsavednote5, textsavednote6; sharedpreferences spnote;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_notes);      savenote = (button)this.findviewbyid(r.id.savenotes);     savenote.setonclicklistener(this);      cleartext = (button)this.findviewbyid(r.id.clearalltext);     cleartext.setonclicklistener(this);      textsavednote1 = (textview)findviewbyid(r.id.stringsavednote1);     textsavednote2 = (textview)findviewbyid(r.id.stringsavednote2);     textsavednote3 = (textview)findviewbyid(r.id.stringsavednote3);     textsavednote4 = (textview)findviewbyid(r.id.stringsavednote4);     textsavednote5 = (textview)findviewbyid(r.id.stringsavednote5);     textsavednote6 = (textview)findviewbyid(r.id.stringsavednote6);      note = (edittext)this.findviewbyid(r.id.notes);      spnote = preferencemanager.getdefaultsharedpreferences(this);     sharedpreferences.editor edit = spnote.edit();     edit.putstring("note"+savenote,note.gettext().tostring());     edit.commit(); }    public boolean oncreateoptionsmenu(menu menu) {     menuinflater inflater = getmenuinflater();     inflater.inflate(r.menu.main, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     switch (item.getitemid()) {     // goes home screen     case r.id.item1:  intent = new intent(notesactivity.this, usersettingactivity.class);     startactivity(i);      case r.id.item2:             intent intent = new intent(this, mainactivity.class);             intent.addflags(intent.flag_activity_clear_top);             startactivity(intent);             return true;     // refreshes page.        case r.id.item3:             finish();             startactivity(getintent());         default:             return super.onoptionsitemselected(item);     } }   public void onclick (view v){     if(v==savenote){         savepreferences("note1", note.gettext().tostring());         loadpreferences();         note.settext("");         if(textsavednote1.gettext().tostring().length()>0){             savepreferences("note2", note.gettext().tostring());             loadpreferences();             note.settext("");         }         else{     } }     else if(v==cleartext){         cleartextviews();     } }  private void savepreferences(string key, string value){     sharedpreferences sharedpreferences = getpreferences(mode_private);     sharedpreferences.editor editor = sharedpreferences.edit();     editor.putstring(key, value);     editor.commit();    }  private void loadpreferences(){     sharedpreferences sharedpreferences = getpreferences(mode_private);     string strsavednote1 = sharedpreferences.getstring("note1", "");     string strsavednote2 = sharedpreferences.getstring("note2", "");     string strsavednote3 = sharedpreferences.getstring("note3", "");     string strsavednote4 = sharedpreferences.getstring("note4", "");     string strsavednote5 = sharedpreferences.getstring("note5", "");     string strsavednote6 = sharedpreferences.getstring("note6", "");     textsavednote1.settext(strsavednote1);     textsavednote2.settext(strsavednote2);     textsavednote3.settext(strsavednote3);     textsavednote4.settext(strsavednote4);     textsavednote5.settext(strsavednote5);     textsavednote6.settext(strsavednote6);    }  private void cleartextviews(){       sharedpreferences sharedpreferences = getpreferences(mode_private);     sharedpreferences.editor editor = sharedpreferences.edit();     editor.clear();     editor.commit();  }  } 

there links can helpfull

how use sharedpreferences in android store, fetch , edit values

http://developer.android.com/reference/android/content/sharedpreferences.html

examples

how use sharedpreferences

http://android-er.blogspot.com.br/2011/01/example-of-using-sharedpreferencesedito.html


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -