2014-01-24 21 views
0

我已经设法让我的分数正确保存到SharedPreferences以及它保存到高分。然而,当检查之前的分数是否比保存的高分更好时,无论如何,它总是会保存它,我不知道为什么。使用sharedpreferences覆盖以前的高分

// 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和高分活动使用此代码阅读:

// 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)); 
    } 
+1

分解你的第一行像这样你可以找出原因:'int currentHighScore = scorePreferences.getInt(“highscore”,0); int currentHighTime = timePreferences.getInt(“hightime”,0); Log.i(“分数”,currentHighScore +“”+ currentHighTime);如果(分数> currentHighScore && time> currentHighTime){' – Tenfour04

+0

更好的时间更好? – Broak

+0

顺便说一句,您不需要为每种类型的条目分开SharedPreferences文件。这就是为什么这些条目有键。 SharedPreferences很像一个HashMap。此外,由于您正在为每个共享首选项的实例使用成员变量,因此您不必在第一次在'onCreate'中执行操作后继续调用getSharedPreference。 – Tenfour04

回答

2

应该这样:

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

...不一样的东西:

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

关键的 “排行榜” 是在你的喜好与相同名称的设置。看起来您正在从“分数”首选项中读取该密钥。它不在那里,所以总是使用默认值0。

+0

是的!从字面上看,现在才意识到这一点。还必须将获取的上下文移动到if之外或者获得空指针。 –

2

看起来你使用相同的密钥为您sharedpreferences这就是为什么值覆盖。 我会推荐使用sqlite来存储最高分。

2

使用一个SharedPreferences对象。为了节省,你可以这样做:

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

然后加载它,也可以使用一个SharedPreferences对象:

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

注:

  • 它使用的首选项键是个好主意文件名和变量,这样可以避免在保存/检索变量时容易出现拼写错误,例如。

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

  • 我没有看到你节省时间和得分,但你可以做同样的方式作为高分和高时间