首先,如果这是重复的话,我很抱歉,我搜索过,但没有找到答案。
我试图在多个活动中保持得分。用户从MainActivity开始,选择要回答的问题并重定向到该活动。用户被授予正确答案的点,并返回到MainActivity以选择新问题。
它正确地对问题进行评分,但它并没有在问题之间保存得分。
主要活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences settings = getSharedPreferences("userScore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("score", userPoints);
editor.commit();
};
Q1 (每个问题的逻辑都相同。只需点击一下按钮即可运行)
private SharedPreferences settings;
private int userPoints;
private int points;
public void submitAnswer (View view) {
if (correctAnswer.isChecked()) {
userPoints = userPoints + 1;
} else {
userPoints = userPoints;
}
settings = getSharedPreferences("userScore", Context.MODE_PRIVATE);
points = settings.getInt("score", userPoints);
Toast.makeText(this, "Answer saved", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Q1.this, MainActivity.class);
startActivity(intent);
}
我对此很新,我知道我错过了一些东西,我只是想不通它是什么。非常感谢任何帮助。
*** **** EDIT
这是调用分数的按钮,它仍然只计算6分中的1分。
private int userPoints;
TextView pointsAwarded;
ImageButton home;
Button check;
int points;
SharedPreferences settings;
check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
settings = getSharedPreferences("userScore", Context.MODE_PRIVATE);
points = settings.getInt("score", userPoints);
pointsAwarded.setText(String.valueOf(points));
}
});
答案 0 :(得分:0)
```
if (correctAnswer.isChecked()) {
userPoints = userPoints + 1;}
``` 在此之后,你应该保存userPoints。也许你忘了。
答案 1 :(得分:0)
代码实际上有一些问题。首先,您要将score
保存在MainActivity
中,或者将其读取。您正在阅读score
中的submitAnswer
或者正在编辑它。
在处理SharedPreferences
时,我通常感到悲观,所以我检查密钥是否已设置为contains()
。
private int points;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences settings = getSharedPreferences("userScore", Context.MODE_PRIVATE);
if(settings.contains("score"){
points = settings.getInt("score", userPoints);
}
else{
Log.e(TAG, "No score yet!");
SharedPreferences.Editor editor = settings.edit();
editor.putInt("score", 0);
editor.commit();
}
}
现在,在问题活动中,您可以阅读SharedPreferences
中的当前点并更新值并保存回SharedPreferences
。
public void submitAnswer (View view) {
if (correctAnswer.isChecked()) {
SharedPreferences settings = getSharedPreferences("userScore", Context.MODE_PRIVATE);
if(settings.contains("score"){
int points = settings.getInt("score", 0);
points++;
SharedPreferences.Editor editor = settings.edit();
editor.putInt("score", points);
editor.commit();
Toast.makeText(this, "Answer saved : points = " + points, Toast.LENGTH_SHORT).show();
}
else{
Log.e(TAG, "No score yet!");
}
}
else{
Toast.makeText(this, "Wrong Answer", Toast.LENGTH_SHORT).show();
}
// There is no need to start the MainActivity through a new Intent just call finish();
finish();
//... and you return to the Activity that is at the top of the stack.
//Intent intent = new Intent(Q1.this, MainActivity.class);
//startActivity(intent);
}
答案 2 :(得分:0)
您必须将当前用户点添加到已保存的点中,然后在添加后保存:
private SharedPreferences settings;
private int userPoints;
private int points;
public void submitAnswer (View view) {
if (correctAnswer.isChecked()) {
userPoints = userPoints + 1;
} else {
userPoints = userPoints;
}
settings = getSharedPreferences("userScore", Context.MODE_PRIVATE);
points = settings.getInt("score", userPoints);
userPoints = userPoints + points ; // addition of current userpoints with saved points
// Now save this points
SharedPreferences.Editor editor = settings.edit();
editor.putInt("score", userPoints );
editor.commit();
Toast.makeText(this, "Answer saved", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Q1.this, MainActivity.class);
startActivity(intent);
}