我遇到共享偏好问题..
在我的应用程序中使用共享首选项来保存测验答案..
我试过了。当我遇到琐碎的问题时,它的工作正常。但我的问题是..
当第一个问题到来时,用户选择一个答案并转到第二个问题,这里用户也会选择答案并进行第三个问题,如明智的过程动作。当用户回来处理常见问题(第二个问题)时,这里的用户回答正确检查按钮,当用户下次到此时,答案没有正确检查......我做错了什么?
请指出我..并提供解决方案如何获得?非常感谢提前..
抱歉我的英文不好..
这是我的代码..
public class MainActivity extends Activity {
RadioGroup btn_practicerg;
RadioButton btn_practice1;
RadioButton btn_practice2;
RadioButton btn_practice3;
RadioButton btn_practice4;
RadioGroup radioButton;
TextView quetxt;
Button next;
Button previous;
int i = 0, k = 0;
int checkedIndex;
int num = 1;
private ArrayList<String> answ1 = new ArrayList<String>();
private ArrayList<String> ques1 = new ArrayList<String>();
final String KEY_SAVED_RADIO_BUTTON_INDEX = "SAVED_RADIO_BUTTON_INDEX";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_practicerg = (RadioGroup) findViewById(R.id.rdgroup);
btn_practice1 = (RadioButton) findViewById(R.id.RB1);
btn_practice2 = (RadioButton) findViewById(R.id.RB2);
btn_practice3 = (RadioButton) findViewById(R.id.RB3);
btn_practice4 = (RadioButton) findViewById(R.id.RB4);
quetxt = (TextView) findViewById(R.id.que_txt);
next = (Button) findViewById(R.id.nxt_btn);
previous = (Button) findViewById(R.id.accbtn);
runOnUiThread(new Runnable() {
public void run() {
LoadQuestions();
}
});
next.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
int selectedId = btn_practicerg.getCheckedRadioButtonId();
// find the radiobutton by returned id
RadioButton radioSexButton = (RadioButton) findViewById(selectedId);
checkedIndex = btn_practicerg.indexOfChild(radioSexButton);
Toast.makeText(MainActivity.this, radioSexButton.getText(),
Toast.LENGTH_SHORT).show();
if (i == ques1.size() - 1) {
showAlert();
} else {
++i;
++num;
TextView quetxt = (TextView) findViewById(R.id.que_txt);
quetxt.setText("Q" + num + ")" + ques1.get(i));
++k;
btn_practice1.setText(answ1.get((k * 4) + 0));
btn_practice2.setText(answ1.get((k * 4) + 1));
btn_practice3.setText(answ1.get((k * 4) + 2));
btn_practice4.setText(answ1.get((k * 4) + 3));
btn_practicerg.clearCheck();
SavePreferences(String.valueOf(num), checkedIndex);
}
}
private void showAlert() {
// TODO Auto-generated method stub
}
});
Button previousbtn1 = (Button) findViewById(R.id.accbtn);
previousbtn1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
--i;
--num;
TextView quetxt = (TextView) findViewById(R.id.que_txt);
quetxt.setText("Q" + num + ")" + ques1.get(i));
--k;
btn_practice1.setText(answ1.get((k * 4) + 0));
btn_practice2.setText(answ1.get((k * 4) + 1));
btn_practice3.setText(answ1.get((k * 4) + 2));
btn_practice4.setText(answ1.get((k * 4) + 3));
LoadPreferences();
}
});
}
private void LoadQuestions() {
ques1.add("whats the name?");
ques1.add("whats place?");
ques1.add("whats the favourite?");
ques1.add("whats the game?");
ques1.add("whats the time?");
answ1.add("A");
answ1.add("B");
answ1.add("C");
answ1.add("D");
answ1.add("MDU");
answ1.add("MS");
answ1.add("CHE");
answ1.add("POND");
answ1.add("1");
answ1.add("2");
answ1.add("3");
answ1.add("4");
answ1.add("VB");
answ1.add("TENN");
answ1.add("HOC");
answ1.add("CRI");
answ1.add("11");
answ1.add("12");
answ1.add("13");
answ1.add("14");
quetxt = (TextView) findViewById(R.id.que_txt);
quetxt.setText("Q" + num + ")" + ques1.get(i));
btn_practice1.setText(answ1.get(0));
btn_practice2.setText(answ1.get(1));
btn_practice3.setText(answ1.get(2));
btn_practice4.setText(answ1.get(3));
}
private void SavePreferences(String key, int value) {
int quest = (Integer.parseInt(key)) - 1;
SharedPreferences preferences = getSharedPreferences("MY_SHARED_PREF",
0);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(String.valueOf(quest), value);
editor.commit();
}
private void LoadPreferences() {
SharedPreferences getProgramPrefs = this.getSharedPreferences(
"MY_SHARED_PREF", MODE_WORLD_READABLE);
int savedRadioIndex = getProgramPrefs.getInt(String.valueOf(num), -1);
System.out.println("SavedRadioIndex" + savedRadioIndex);
RadioButton savedCheckedRadioButton = (RadioButton) btn_practicerg
.getChildAt(savedRadioIndex);
savedCheckedRadioButton.setChecked(true);
}
OnCheckedChangeListener radioGroupOnCheckedChangeListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = (RadioButton) group
.findViewById(checkedId);
checkedIndex = group.indexOfChild(radioButton);
System.out.println("checkedIndex" + checkedIndex);
}
};
}
答案 0 :(得分:0)
将此类数据保存在首选项中并不好。如果要在应用程序完成后再次启动时获取数据,最好将此数据保存在数据库中。但在这种情况下,您可以将数据存储在活动中的某个对象中,如下所示:
public class Question {
private int id;
private String question;
private List<Answer> possibleAnswers;
private int correctAnswerId;
private String userAnswer;
}
答案 1 :(得分:0)
您是否尝试过调试以在SavePreferences方法上观察“quest”变量的值?从String到int以及从int到String再次有两个不必要的强制转换,也许这可能是问题,具体取决于“key”变量的值。
您应该尝试直接使用“key”变量作为编辑器的键而不进行强制转换。
其余功能似乎是正确的。
修改强> 我所说的可能是这个演员阵容有问题,但我不确定。
private void SavePreferences(String key, int value) {
SharedPreferences preferences = getSharedPreferences("MY_SHARED_PREF",0);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(key, value);
editor.commit();
}