shared preferences for storing a single score

喜你入骨 提交于 2019-12-24 03:51:19

问题


I am developing this quiz game. at the end of the game I show the user a detailed total score. What I want to develop here is a using sharedpreferences to store the value, be able to retrieve it using other activity showing the user's total score. I am only interested in storing one score for the quiz (initially). The reason I want to implement this is that in the future I will apply this for other quiz modes and show the total score for all quizzes in one activity called highscores. below is my code, and the variable finalscore(int) is storing the score of the user. My code is a mess but I can't quite figure this out so if anyone can help me with the implementation of shared preferences using my code I would appreciate it a lot, in order to get a better understanding.

Update 3.0

public static final String PREFS_FILE = "prefsFile";
SharedPreferences sharedPref = this.getSharedPreferences(PREFS_FILE,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putInt(PREFS_FILE, finalScore);
        editor.commit();

TotalScore Activity

public class TotalScore extends Activity {
TextView easy, totalScoreHeading;

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

    totalScoreHeading = (TextView) findViewById(R.id.totalScoreHeading);
    easy = (TextView) findViewById(R.id.txtViewTotalScoresEasy);

    totalScoreHeading.setTextSize(28);

    SharedPreferences sharedPref = getSharedPreferences(ScoreActivity.PREFS_FILE, Context.MODE_PRIVATE);
    int defaultValue = 0;
    int highscore = sharedPref.getInt(ScoreActivity.PREFS_FILE, defaultValue);
    easy.setText("" + highscore);

}

}


回答1:


You are using finalScoreString's value as key to store final score in shared preferences. This variable is not available in your other activity, hence your problem. It would be advisable to follow the good practice of defining a key as:

public static final String FINAL_SCORE = "finalScore";

Do this in your main activity, where you save score

editor.putInt(FINAL_SCORE, finalScore);

Then, in other activity, use it to retrieve score:

int highscore = sharedPref.getInt(MainActivity.FINAL_SCORE, defaultValue)

As a matter of fact, you should also use a static key for Shared Preferences file name, instead of hardcoded "LevelScores" string.

This will also mitigate another problem you might have, with the following lines:

finalScore = timeLeft * QuizActivity.correct;
finalScoreString = String.valueOf(finalScore);

With this, whatever the value of finalScore, is being used as key to store final score in preferences. But this value might (and probably will) be different each time, meaning that your key will also change. In the end, you would have lots of entries with different keys in preferences. How would you know which one you want to use to get the corresponding value? By using public static key (or keys for different game modes foe example), your final score(s) could be easily retrieved, because you know exactly which key to use.



来源:https://stackoverflow.com/questions/28918571/shared-preferences-for-storing-a-single-score

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