ulyssessPax
ulyssessPax

Reputation: 199

How Save a int variable with SharedPreferences?

Good day people,

I have a problem I would save whith SharedPreference simple int value variable, but I am having difficulty. I have a Flag-Variable "signum" that changes the value based on the made choice ImageButton, I would save this Flag-Value. It's possible in one Activity use twice method SharedPreferences? For me it's not important if the Flag-Value is a String or int variable, I need a easy way. I think(but I'm not sure even of this) I'm in the wrong store in the code-line I'm in the code line : salvum2.putInt("semper2", 1 or 2); insert the right way Thaks for all and for your help!

public class MainActivity extends Activity {
private static final String MUTATIO = "semper";
private static final String MUTATIO2 = "semper2";
private ImageButton button;
int signum ;  //this is the variable that I want to 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 I would save the 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 a 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();
            }}});}}

Upvotes: 0

Views: 1199

Answers (3)

Mr. Borad
Mr. Borad

Reputation: 391

SharedPreferences prefs = getSharedPreferences(MUTATIO, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putInt("semper", R.drawable.lux );
editor.putInt("semper2", 1);
editor.commit();

Upvotes: 2

Bhanu Sharma
Bhanu Sharma

Reputation: 5145

to save use this

private SharedPreferences prefs;
 prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
 prefs.edit().putInt("userId", userId).commit();
prefsCommit();

and for getting use this

int userId = prefs.getInt("userId", 0);

Upvotes: 0

Phantômaxx
Phantômaxx

Reputation: 38098

If semper2 is a STRING, then this is wrong (all occurrences): salvum2.putString("semper2", 2);
it should be: salvum2.putString("semper2", "2");

If semper2 is an INT, then this is wrong (all occurrences): salvum2.putString("semper2", 2);
it should be: salvum2.putInt("semper2", 2);

Also note that you don't need to create an editor for each preference key you want to save: one is enough.
Commit the operations once, at the end of all your savings.

Upvotes: 2

Related Questions