0

我面临着共享偏好的问题..

在我的应用程序中,我使用共享偏好来保存测验答案..

我努力了。当我遇到棘手的问题时,它的工作正常……但我的问题是……

当第一个问题出现时,用户选择一个答案并进入第二个问题,这里用户也将选择答案并进入第三个问题,就像明智的流程移动一样。当用户回来回答前一个问题(第二个问题)时,用户在这里回答检查按钮正确,当用户到下一个时,答案没有正确检查......我做错了什么?

请指出我..并给出解决方案如何得到它?提前非常感谢..

对不起,我的英语不好..

这是我的代码..

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);
        }
    };

}
4

2 回答 2

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();
}
于 2013-08-21T07:48:20.143 回答
0

将此类数据保存在首选项中并不好。如果您想在应用程序完成然后重新启动时获取数据,最好将这些数据保存在数据库中。但在这种情况下,您可以将数据存储在活动中的某个对象中,如下所示:

public class Question {
private int id;
private String question;
private List<Answer> possibleAnswers;
private int correctAnswerId;
private String userAnswer;
}
于 2013-08-21T07:37:46.710 回答