用于存储单个乐谱的共享首选项 [英] shared preferences for storing a single score

查看:33
本文介绍了用于存储单个乐谱的共享首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发这个问答游戏.在游戏结束时,我会向用户显示详细的总分.我想在这里开发的是使用共享首选项来存储值,能够使用显示用户总分的其他活动来检索它.我只对为测验存储一个分数感兴趣(最初).我想实现这一点的原因是,将来我会将其应用于其他测验模式,并在一项称为高分的活动中显示所有测验的总分.下面是我的代码,变量 finalscore(int) 存储用户的分数.我的代码一团糟,但我无法弄清楚这一点,所以如果有人可以帮助我使用我的代码实现共享首选项,我将不胜感激,以便更好地理解.

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.

更新 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 活动

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);

}

}

推荐答案

您正在使用 finalScoreString 的 值作为键来存储共享首选项中的最终分数.此变量在您的其他活动中不可用,因此是您的问题.建议遵循将密钥定义为的良好做法:

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)

事实上,您还应该对共享首选项文件名使用静态键,而不是硬编码的LevelScores"字符串.

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);

这样,无论 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.

这篇关于用于存储单个乐谱的共享首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆