android - HOW TO USE SHARED PREFERENCES IN FRAGMENT ACTIVITY -


i'm facing problem while use code in fragment , did few changes of help..but still have few errors..

this code want transfer in fragment activity normal activity..

public class mainactivity extends activity {  final static string shared_name_string="sharedp"; final static string user_name_string="user";  button button; edittext edittext;  sharedpreferences sharedpreferences;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     edittext=(edittext) findviewbyid(r.id.usernameedittext);     button=(button) findviewbyid(r.id.enterbutton);      log.d("dictionary", "main activity started");       sharedpreferences=getsharedpreferences(shared_name_string, mode_private);     string usernamestring=sharedpreferences.getstring(user_name_string, "");      edittext.settext(usernamestring);      button.setonclicklistener(new onclicklistener() {          @override         public void onclick(view v) {             string string=edittext.gettext().tostring();             intent intent=new intent(mainactivity.this, dictionarylistactivity.class);             intent.putextra("user", string);              sharedpreferences.editor editor=sharedpreferences.edit();             editor.putstring(user_name_string, string);             editor.commit();              startactivity(intent);          }     });  }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.main, menu);     return true; } 

and showing error mode_private cannot resolve variable.

so changed context.mode_private,then shows unreachable code on line of "edittext=(edittext) findviewbyid(r.id.usernameedittext);"

how rectify ?

this fragment looks likes :

public class fragmenta extends fragment {  final static string shared_name_string="sharedp"; final static string user_name_string="user";  button button; edittext edittext; context c;  sharedpreferences sharedpreferences;   public fragmenta() {     // required empty public constructor }  @override public view oncreateview(layoutinflater inflater, viewgroup container,         bundle savedinstancestate) {     // inflate layout fragment     return inflater.inflate(r.layout.fragment_a, container, false);     edittext=(edittext) getview().findviewbyid(r.id.usernameedittext);     button=(button) getview().findviewbyid(r.id.enterbutton);      log.d("dictionary", "main activity started");      context c =getactivity();     sharedpreferences=this.c.getsharedpreferences(shared_name_string,mode_private);     string usernamestring=sharedpreferences.getstring(user_name_string, "");      edittext.settext(usernamestring);      button.setonclicklistener(new onclicklistener() {          @override         public void onclick(view v) {             string string=edittext.gettext().tostring();             intent intent=new intent("android.intent.action.jjjj");             intent.putextra("user", string);              sharedpreferences.editor editor=sharedpreferences.edit();             editor.putstring(user_name_string, string);             editor.commit();              startactivity(intent);          }     }); }    } 

simply , replace

getsharedpreferences(shared_name_string,context.mode_private); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -