我想我现在有一个功能齐全的琐事游戏。在结果页面上的游戏结束时,它会显示分数(long)和百分比(int)。我现在想实现一个离线 SharePreferences highscores,其中输入了 3 个变量——rank(int)、score(long) 和 percent(int)。我试图首先用一个变量实现一个高分,然后再添加另外两个,但我似乎失败得很厉害。您可以在下面的我的活动中看到很多注释掉的代码,其中我正在尝试一些东西,并且许多其他尝试已经被删除。
这是我第一次尝试使用 SharedPreferences,所以我还没有完全掌握它。
结果.java
public class Results extends Activity {
public static final String SP_NAME = "TEST";
public static final String INT_PERCENTAGE = "SP_percentage";
public static final String INT_RANK = "SP_rank";
public static final String LONG_SCORE = "SP_score";
private SharedPreferences mPrefs;
QuestionView qv = new QuestionView();
ArrayList<Question> queryList = qv.getQueries();
int cAnswers, wAnswers, percentage, rank;
long score;
ArrayList<Question> qs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resultsmain);
mPrefs = getSharedPreferences(SP_NAME, 0);
cAnswers = getIntent().getIntExtra("correctAnswers", -1);
wAnswers = getIntent().getIntExtra("wrongAnswers", -1);
score = getIntent().getLongExtra("score", -1);
qs = getIntent().getParcelableArrayListExtra("queries");
Button mainmenuBtn = (Button)findViewById(R.id.mainmenuBtn);
mainmenuBtn.setText("Main Menu");
mainmenuBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
restart();
}
});
//Retrieving high score
SharedPreferences mPrefs = getSharedPreferences(LONG_SCORE, 0);
SharePreferences.Editor editor = prefs.edit();
long highscore = mPrefs.getLong(LONG_SCORE, 0);
//Saving current score as high score
/* SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInteger(HIGH_SCORE, currentScore);*/
// Commit the edits!
editor.commit();
SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);
/* SharedPreferences.Editor editor = prefs.edit();
return editor.commit();*/
showResults();
}
//...other code
}