初始化高分陣列:共享偏好不保存數據
score = 0;
sharedPreferences = context.getSharedPreferences("Scores", Context.MODE_PRIVATE);
//initialize the array of high scores
highScore[0] = sharedPreferences.getInt("score1",0);
highScore[1] = sharedPreferences.getInt("score2",0);
highScore[2] = sharedPreferences.getInt("score3",0);
highScore[3] = sharedPreferences.getInt("score4",0);
highScore[4] = sharedPreferences.getInt("score5",0);
檢查對於4個最高值:
highScore[5] = score;
Arrays.sort(highScore);
這是我在共享偏好保存數據代碼
SharedPreferences.Editor e = sharedPreferences.edit();
for(int j=4;j>=0;j--){
e.putInt("score"+(j+1),highScore[j]);
e.apply();
}
請在這裏檢查:http://stackoverflow.com/questions/23024831/android-shared-preferences-example –
這是完整的代碼保存價值的偏好? –
你是否用關鍵「分數」初始化了sharedPreferences? – Madhav