Java 使用SharedReferences覆盖以前的高分

Java 使用SharedReferences覆盖以前的高分,java,android,Java,Android,我成功地将我的分数保存到SharedReferences,并将其保存到highscore。然而,当检查前一个分数是否比保存的高分好时,不管发生什么,它总是会保存下来,我不知道为什么 // save score and time if current score is > than current highscore and time is > than current hightime if (score > scorePreferences.getInt("h

我成功地将我的分数保存到SharedReferences,并将其保存到highscore。然而,当检查前一个分数是否比保存的高分好时,不管发生什么,它总是会保存下来,我不知道为什么

// save score and time if current score is > than current highscore and time is > than current hightime
        if (score > scorePreferences.getInt("highscore", 0) && time > timePreferences.getInt("hightime", 0)) {
            highscorePreferences = getContext().getSharedPreferences("highscore", 0);
            SharedPreferences.Editor editorHighscore = highscorePreferences.edit();
            editorHighscore.putInt("highscore", score);
            editorHighscore.commit();

            timePreferences = getContext().getSharedPreferences("hightime", 0);
            SharedPreferences.Editor editorHightime = timePreferences.edit();
            editorHightime.putInt("hightime", time);
            editorHightime.commit();
        }
然后使用以下代码读取gameoveractivity和highscore activity:

// load score from last session
    private void load() {
        // get score and set text field
        scorePreferences = getSharedPreferences("score", 0);        
        score = scorePreferences.getInt("score", 0);
        scoreValue.setText(Integer.toString(score));

        // get time and set text field
        timePreferences = getSharedPreferences("time", 0);
        time = timePreferences.getInt("time", 0);
        timeValue.setText(Integer.toString(time) + " seconds");

        // get highscore and set text field
        highscorePreferences = getSharedPreferences("highscore", 0);
        highscore = highscorePreferences.getInt("highscore", 0);
        highscoreValue.setText(Integer.toString(highscore));
    }

看起来您对SharedReferences使用了相同的键,这就是值覆盖的原因。
我建议使用sqlite来存储最高分。

看起来您对SharedReference使用了相同的键,这就是为什么这些值会覆盖。 我建议使用sqlite存储最高分。

如果:

if (score > scorePreferences.getInt("highscore", 0)...
…不是这样的:

if (score > highscorePreferences.getInt("highscore", 0)...
“highscore”键位于您的首选项集中,并具有相同的名称。看起来您正在从“分数”首选项中读取该键。它不在那里,因此始终使用默认值0。

应:

if (score > scorePreferences.getInt("highscore", 0)...
…不是这样的:

if (score > highscorePreferences.getInt("highscore", 0)...

“highscore”键位于您的首选项集中,并具有相同的名称。看起来您正在从“分数”首选项中读取该键。它不存在,因此始终使用默认值0。

使用一个SharedReferences对象。要保存,可以执行以下操作:

SharedPreferences prefs = getSharedPreferences("score", Context.MODE_PRIVATE);

int highscore = prefs.getInt("highscore", 0);
int hightime = prefs.getInt("hightime", 0);

if (score > highscore && time > hightime) {
    SharedPreferences.Editor editor = prefs.editor();

    editor.putInt("highscore", score);
    editor.putInt("hightime", time);
    editor.commit();
}
然后加载它,并使用一个SharedReferences对象:

private void load() {
    SharedPreferences prefs = getSharedPreferences("score", Context.MODE_PRIVATE);

    score = prefs.getInt("score", 0);
    scoreValue.setText(Integer.toString(score));

    time = prefs.getInt("time", 0);
    timeValue.setText(Integer.toString(time) + " seconds");

    highscore = prefs.getInt("highscore", 0);
    highscoreValue.setText(Integer.toString(highscore));
}       
注:

  • 最好使用prefs文件名和变量的键,这样在保存/检索变量时可以避免容易的打字错误,例如

    public static final String PREFS_NAME = "score";
    public static final String KEY_HIGHSCORE = "highscore";
    public static final String KEY_HIGHTIME = "hightime";
    
然后使用它

SharedPreferences prefs = getSharedPreferences(ClassNameWhereItsDeclared.PREFS_NAME, Context.MODE_PRIVATE);

editor.putInt(ClassNameWhereItsDeclared.KEY_HIGHSCORE, score);
等等

  • 我看不出你在哪里节省时间和分数,但你可以用与highscore和hightime相同的方法来做

使用一个SharedReferences对象。要保存,可以执行以下操作:

SharedPreferences prefs = getSharedPreferences("score", Context.MODE_PRIVATE);

int highscore = prefs.getInt("highscore", 0);
int hightime = prefs.getInt("hightime", 0);

if (score > highscore && time > hightime) {
    SharedPreferences.Editor editor = prefs.editor();

    editor.putInt("highscore", score);
    editor.putInt("hightime", time);
    editor.commit();
}
然后加载它,并使用一个SharedReferences对象:

private void load() {
    SharedPreferences prefs = getSharedPreferences("score", Context.MODE_PRIVATE);

    score = prefs.getInt("score", 0);
    scoreValue.setText(Integer.toString(score));

    time = prefs.getInt("time", 0);
    timeValue.setText(Integer.toString(time) + " seconds");

    highscore = prefs.getInt("highscore", 0);
    highscoreValue.setText(Integer.toString(highscore));
}       
注:

  • 最好使用prefs文件名和变量的键,这样在保存/检索变量时可以避免容易的打字错误,例如

    public static final String PREFS_NAME = "score";
    public static final String KEY_HIGHSCORE = "highscore";
    public static final String KEY_HIGHTIME = "hightime";
    
然后使用它

SharedPreferences prefs = getSharedPreferences(ClassNameWhereItsDeclared.PREFS_NAME, Context.MODE_PRIVATE);

editor.putInt(ClassNameWhereItsDeclared.KEY_HIGHSCORE, score);
等等

  • 我看不出你在哪里节省时间和分数,但你可以用与highscore和hightime相同的方法来做

像这样分解第一行,以便找出原因:
int currentHighScore=scorePreferences.getInt(“highscore”,0);int currentHighTime=timePreferences.getInt(“hightime”,0);Log.i(“分数”,currentHighScore+“”+currentHighTime);如果(分数>当前高分数和时间>当前高时间){
时间越长越好吗?顺便说一句,对于每种类型的条目,您不需要单独的SharedReferences文件。这就是为什么条目都有键的原因。SharedReferences很像HashMap。此外,由于您对共享首选项的每个实例都使用了成员变量,因此不必在第一次在
onCreate
中执行此操作时,请像这样分解第一行,以便找出原因:
int currentHighScore=scorePreferences.getInt(“highscore”,0);int currentHighTime=timePreferences.getInt(“hightime”,0);Log.i(“scores”,currentHighScore+”+currentHighTime);如果(分数>当前高分数和时间>当前高时间){
时间越长越好吗?顺便说一句,对于每种类型的条目,您不需要单独的SharedReferences文件。这就是为什么条目都有键的原因。SharedReferences很像HashMap。此外,由于您对共享首选项的每个实例都使用了成员变量,因此不必在第一次在
onCreate
中执行此操作时。是的!现在才意识到这一点。还必须将获取上下文移动到if或else获得空指针的外部。是的!现在才意识到这一点。还必须将获取上下文移动到if或else获得空指针的外部。