如何使用hashmap来保存和检索单选按钮值

时间:2013-08-23 08:03:32

标签: android radio-button hashmap

我遇到了单选按钮值的问题,保存并检索它..

我尝试过共享偏好设置..它可以正确保存但在检索时遇到问题......

任何人都可以帮我了解如何保存单选按钮值并检索..我的key是问题编号。我需要保存每个问题编号的答案。问题编号的变量是int num = 0;

当我前进和后退时,根据问题编号,标记即保存的答案应为checked()。如何使用HashMap实现?

提前多多感谢..

抱歉我的英文不好..

这是我的代码..

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

        @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) {

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

                                }

                        }

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

        }

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

0 个答案:

没有答案