android - How Save a int variable with SharedPreferences? -


good day people,

i have problem save whith sharedpreference simple int value variable, having difficulty. have flag-variable "signum" changes value based on made choice imagebutton, save flag-value. it's possible in 1 activity use twice method sharedpreferences? me it's not important if flag-value string or int variable, need easy way. think(but i'm not sure of this) i'm in wrong store in code-line i'm in code line : salvum2.putint("semper2", 1 or 2); insert right way thaks , help!

public class mainactivity extends activity { private static final string mutatio = "semper"; private static final string mutatio2 = "semper2"; private imagebutton button; int signum ;  //this variable want save  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      choicheimagebutton();     operation(); }   private void choicheimagebutton() {     final sharedpreferences optio =                      getsharedpreferences(mutatio, context.mode_private);      final sharedpreferences optio2 =              getsharedpreferences(mutatio2, context.mode_private);      button = (imagebutton) findviewbyid(r.id.imagebutton1);     button.setimageresource(optio.getint(mutatio, r.drawable.default));       button.setonlongclicklistener(new onlongclicklistener(){          @override         public boolean onlongclick(view v) {     final dialog dialog = new dialog(mainactivity.this);             dialog.setcontentview(r.layout.dialog);             dialog.settitle("titolo menĂ¹ scelta");                textview text = (textview) dialog.findviewbyid(r.id.text);             text.settext("scegli l'icona");             imageview image = (imageview) dialog.findviewbyid(r.id.image);             image.setimageresource(r.drawable.ic_launcher);              dialog.show();              button dialogbuttonluce = (button) dialog.findviewbyid(r.id.dialogbuttonluceid);              dialogbuttonluce.setonclicklistener(new onclicklistener() {                 @override                 public void onclick(view v) {                       button.setimageresource(r.drawable.lux);                     signum = 1 ;     sharedpreferences.editor salvum = optio.edit();     salvum.putint("semper", r.drawable.lux);           //now save flag-int-variable      salvum.putint("semper", 1);     salvum.commit();                     dialog.dismiss();                  }             });     button dialogbuttongas = (button) dialog.findviewbyid(r.id.dialogbuttongasid);      dialogbuttongas.setonclicklistener(new onclicklistener(){              @override             public void onclick(view v) {                  button.setimageresource(r.drawable.gas);                 signum = 2;                 sharedpreferences.editor salvum = optio.edit();                 salvum.putint("semper", r.drawable.gas);                 salvum.commit();                    salvum.putint("semper", 2);                 salvum.commit();  dialog.dismiss();}});   button reset = (button) dialog.findviewbyid(r.id.dialogripristinaid);  reset.setonclicklistener(new onclicklistener(){  @override public void onclick(view v) {     optio.edit().clear().commit();      dialog.dismiss();     }});return true;}});}  private void operation() {     button.setonclicklistener(new onclicklistener(){          @override         public void onclick(view v) {              if (signum==1) {                 toast.maketext(mainactivity.this, "i have lux", toast.length_long)                 .show();                 }             if (signum==2) {                 toast.maketext(mainactivity.this, "i have gas gas", toast.length_long)                 .show();             }             else  {                 toast.maketext(mainactivity.this, "vacuum", toast.length_long)                 .show();             }}});}} 

sharedpreferences prefs = getsharedpreferences(mutatio, context.mode_private); editor editor = prefs.edit(); editor.putint("semper", r.drawable.lux ); editor.putint("semper2", 1); editor.commit(); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -