SharedPreferences两次保存数据

时间:2015-09-02 20:18:54

标签: java android arraylist sharedpreferences

private void setHighScore(){
    SharedPreferences.Editor scoreEdit = gamePrefs.edit();
    DateFormat dateForm = new SimpleDateFormat("MM/dd/yy");
    String dateOutput = dateForm.format(new Date());
    String scores = gamePrefs.getString("highScores", "");
    if(scores.length() > 0) {
        List<Score> scoreStrings = new ArrayList<Score>();
        String[] exScores = scores.split("\\|");
        for(String eSc : exScores){
            String[] parts = eSc.split(" - ");
            scoreStrings.add(new Score(parts[0], Integer.parseInt(parts[1])));
        }
        Score newScore = new Score(dateOutput, score);
        scoreStrings.add(newScore);
        Collections.sort(scoreStrings);
        StringBuilder scoreBuild = new StringBuilder("");
        for (int x = 0; x < scoreStrings.size(); x++){
            if(x >= 10) break;
            if(x > 0) scoreBuild.append("|");
            scoreBuild.append(scoreStrings.get(x).getScoreText());
        }
        scoreEdit.putString("highScores", scoreBuild.toString());
        scoreEdit.commit();
    }else{
        scoreEdit.putString("highScores", ""+dateOutput+ " - " + score);
        scoreEdit.commit();
    }
}

public class MyCount extends CountDownTimer{
    public MyCount(long millisInFuture,long countDownInterval){
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish() {
        final TextView time = (TextView) findViewById(R.id.time);
        time.setText("Times Up!");
        try {
            Thread.sleep(250);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        setHighScore();
        Intent intent = new Intent(getApplicationContext(), score_screen.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onTick(long millisUntilFinished){
        final TextView time = (TextView) findViewById(R.id.time);
        time.setText("Left: " + millisUntilFinished/1000);
    }
}
public class score_screen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score_screen);
        TextView scoreView = (TextView)findViewById(R.id.scoreView);
        ImageButton home = (ImageButton)findViewById(R.id.home);
        SharedPreferences scorePrefs = getSharedPreferences(Game.GAME_PREFS, 0);
        String[] savedScores = scorePrefs.getString("highScores", "").split("\\|");
        StringBuilder scoreBuild = new StringBuilder("");
        for(String score : savedScores) {
            scoreBuild.append(score+"\n");
        }
        scoreView.setText(scoreBuild.toString());

        home.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), ColorMatch.class);
                startActivity(intent);
            }
        });
    }
}

我试图在我的计时器启动后从我的游戏屏幕保存高分。我能够做到这一点很好,然后当我第二次访问我的ArrayList时,最后一个分数被重新保存,我无法弄清楚为什么会发生这种情况。

例如,在一场比赛结束后,得分为3170,我将进入高分屏幕,在那里我只看到一个得分实例。然后,如果我玩新游戏,或从主菜单打开我的Highscores屏幕,我现在看到两个相同分数的实例。 (3170)我的猜测是得分是两倍,但我在高分屏幕上找不到第二个.commit()。

2 个答案:

答案 0 :(得分:0)

实际上,您可能无法将高分保存两次。我认为问题在于你正在追加高分。

您需要将高分插入数组中的某个位置,而不是追加高分。 Java数组虽然是固定大小,但您可能需要考虑使用ArrayList或LinkedList。

答案 1 :(得分:0)

在我没有发布的方法中,(OnDestroy())如果有人退出,我有方法setHighScore()的第二个实例来存储分数。当我删除它时,它停止了双重写入让我相信问题是finish()引用onDestroy()来关闭活动。