简体   繁体   中英

Save value of Int (Shared Preference)

I'm new to java. I've made a counter which goes up as user holds on a button. I want the app to start with int value of where it left. I know SharedPreference is the way to go but I've no idea how to use it. I'm not sure where to put which part of SharedPreference. Thank you.

public class MainActivity extends AppCompatActivity {
Button button;
int count = 1;
TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button);
    text = (TextView) findViewById(R.id.textView);

    button.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
           count++;
            text.setText(String.valueOf(count));
        return false;
        }
    });

}

}

Add the following function to your activity

 public int getValue(String key) {
        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        int value = sharedPref.getInt(key, 0);
        return value;
 }
 public void saveValue(String key, int value) {
        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putInt(key, value);
        editor.commit();
 }

Some code added in your onCreate() method

final String key = "somekey";
count  = getValue(key); //get value from sharedPreference
button = (Button) findViewById(R.id.button);
text = (TextView) findViewById(R.id.textView);
text.setText(String.valueOf(count)); // set it first
button.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View view) {
        count++;
        saveValue(key,count);
        text.setText(String.valueOf(count));
        return false;
    }
});

Try turning

int count = 1;

into

static int count = 1;

I'm also somewhat a Java noobie so this may or may not work.

You can do it like this,save count in SharedPreference when destroy the activity and read value from SharedPreference when you create it:

public class MainActivity extends AppCompatActivity {
    Button button;
    int count = 1;
    TextView text;
    SharedPreferences sh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        text = (TextView) findViewById(R.id.textView);

        sh = getSharedPreferences("sh_name", MODE_PRIVATE);
        count = sh.getInt("count", 1);

        button.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                count++;
                text.setText(String.valueOf(count));
                return false;
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        sh.edit().putInt("count", count).apply();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM