private void init() {
SharedPreferences prefs = getSharedPreferences("data", Context.MODE_PRIVATE);
// SET VALUE RECORD
record = prefs.getInt("record", 0);
prefs.edit().commit();
}
private void setRecord(int i) {
SharedPreferences prefs = getSharedPreferences("data", Context.MODE_PRIVATE);
if(i > prefs.getInt("record", 0))
prefs.edit().putInt("record", i);
prefs.edit().commit();
}
private int getRecord() {
int rec;
SharedPreferences prefs = getSharedPreferences("data", Context.MODE_PRIVATE);
rec = prefs.getInt("record", 0);
prefs.edit().commit();
Toast toast = Toast.makeText(this, rec+"", Toast.LENGTH_SHORT);
toast.show();
return rec;
}
此代碼應設置一個int和檢索它,但它似乎沒有以往任何時候都設置......你可以看到這是爲什麼?SharedPreferences工作不正常,不能讀也不會寫的喜好
if(i> prefs.getInt(「record」,0)) 對於這個工作,你必須在之前放置一條記錄。你在哪裏做的? – nedaRM