How to save score to SharedPreferences then update it?

≡放荡痞女 提交于 2019-12-22 00:43:03

问题


I have an app which shows the total earnings of stars in the MainActivity as textview. I want to do is input 0 if the app is run for the first time. Then afterwards as I finish a level and earn some stars (like currentScore + 50 = newScore) it will be redirected to MainActivity with the newScore textView.

This is my MainActivity where the the textview shows the score

public class MainActivity extends Activity {

  private static TextView txt_stars;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    updateStars();
  }

    public void updateStars() {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt("Stars", 0);
    editor.commit();
    txt_stars = (TextView) findViewById(R.id.txtStars);
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        //the key is "Stars" don't forget to type it as you created the Sp
    String stars = String.valueOf(sharedPreferences.getInt("Stars", 0));
    txt_stars.setText(stars);
}
}

And for the activity where I read the current score then add 50 to it then update

public void onClick(DialogInterface dialog, int whichButton) {

     SharedPreferences sharedPreferences = 
     PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
     Integer newstars = sharedPreferences.getInt("Stars", 0);
     Integer totalStars = newstars + 50;
     SharedPreferences.Editor editor = sharedPreferences.edit();
     editor.putInt("Stars", totalStars);
     editor.commit();

  //please help me improve my code here

    Intent nextForm = new Intent(.MainActivity");
    startActivity(nextForm);

  }

And also can a SharedPreferences be use in different activities? Like I just save a score from ActivityOne and can be read in ActivityTwo?

Thanks so much!


回答1:


Well, you should first read the SharedPreferences documentation, and then follow my steps to save values with SharedPreferences

Your UpdateStars() method should look like this :

public class updateStars() {
txt_stars = (TextView) findViewById(R.id.txtStars);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
//the key is "Stars" don't forget to type it as you created the Sp
String stars = String.valueOf(sharedPreferences.getInt("Stars", 0));
txt_stars.setText(stars);
}

And everytime you want to keep the users Stars you'll have to use the putInt() :

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("Stars", YOUR_INT_VALUE); //Here you save the stars of the user
editor.commit();



回答2:


I once wrote a useful SharedPreferences class to write/retrieve data easily, which you can be copy/pasted from here:

https://github.com/asystat/bus_locator/blob/master/benasque2014/Autobus/src/com/example/com/benasque2014/mercurio/KeyStoreController.java

Then you write like this:

KeyStoreController.getKeyStore().setPreference("yourKey", yourValue)

yourValue can be int, boolean, long or string

and to retrieve:

KeyStoreController.getKeyStore().getInt("yourKey", defaultValue)
KeyStoreController.getKeyStore().getLong("yourKey", defaultValue)
KeyStoreController.getKeyStore().getString("yourKey", defaultValue)
KeyStoreController.getKeyStore().getBoolean("yourKey", defaultValue)

You can also call KeyStoreController.getKeyStore().appOpened() in the onCreate method of your MainApplication or Launcher Activity, and then you can use getTimesOpened() and isFirstLaunch() for any feature you'd like to add to your app.

I hope it can be useful to someone.



来源:https://stackoverflow.com/questions/35069461/how-to-save-score-to-sharedpreferences-then-update-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!