如何从android中动态创建的多个无线电组中获取正确的单选按钮索引

时间:2016-01-20 11:30:34

标签: android

我正在创建一个测验android应用程序,其中我将从数据库中获取问题和答案的选项在数组中指定是一个表格行,每个问题有两个无线电组,因为有四个选项。 它按以下方式完成:

for(int i=0;i<question_array.size();i++)
    {


        View tr = (LinearLayout) View.inflate(QuestionsActivity.this,R.layout.question_row, null);

        TextView txt_question = (TextView) tr.findViewById(R.id.txt_question);



        RadioButton rad1 = (RadioButton) tr.findViewById(R.id.radio1);
        RadioButton rad2 = (RadioButton) tr.findViewById(R.id.radio2);
        RadioButton rad3 = (RadioButton) tr.findViewById(R.id.radio3);
        RadioButton rad4 = (RadioButton) tr.findViewById(R.id.radio4);

        txt_question.setText(question_array.get(i));

        rad1.setText(answer_array.get(i).get("A1"));
        rad2.setText(answer_array.get(i).get("A2"));
        rad3.setText(answer_array.get(i).get("A3"));
        rad4.setText(answer_array.get(i).get("A4"));

        RadioGroup rg1 = (RadioGroup) tr.findViewById(R.id.rg1);
        RadioGroup rg2 = (RadioGroup) tr.findViewById(R.id.rg2);

        rg1.clearCheck(); // this is so we can start fresh, with no selection on both RadioGroups
        rg2.clearCheck();
        rg1.setOnCheckedChangeListener(listener1);
        rg2.setOnCheckedChangeListener(listener2);

        ll_ques_answr_row.addView(tr);

    }

当我尝试获取所选选项的索引时......对于第一个问题,我得到了正确的索引..但对于所有其他问题,索引将变为-1。 两个广播组的两名听众如下:

private OnCheckedChangeListener listener1 = new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId != -1) {

            TableRow tr1 = (TableRow) group.getParent();

            TableLayout tl = (TableLayout) tr1.getParent();

            TableRow tr2=  (TableRow) tl.getChildAt(1);

            RadioGroup rg2 = (RadioGroup) tr2.getChildAt(0);

            int index = group.indexOfChild(findViewById(group.getCheckedRadioButtonId()));
              Log.e("index1", String.valueOf(index));
            rg2.setOnCheckedChangeListener(null); // remove the listener before clearing so we don't throw that stackoverflow exception(like Vladimir Volodin pointed out)
            rg2.clearCheck(); // clear the second RadioGroup!
            rg2.setOnCheckedChangeListener(listener2); //reset the listener


        }
    }
};

private OnCheckedChangeListener listener2 = new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId != -1) {

            TableRow tr1 = (TableRow) group.getParent();

            TableLayout tl = (TableLayout) tr1.getParent();

            TableRow tr2=  (TableRow) tl.getChildAt(0);

            RadioGroup rg1 = (RadioGroup) tr2.getChildAt(0);
            int index = group.indexOfChild(findViewById(group.getCheckedRadioButtonId()));
            Log.e("index2", String.valueOf(index));
            rg1.setOnCheckedChangeListener(null);
            rg1.clearCheck();
            rg1.setOnCheckedChangeListener(listener1);


        }
    }
};

有人可以建议我如何获取正确的单选按钮ID?我究竟做错了什么?

 asdf

请帮忙!提前谢谢!

1 个答案:

答案 0 :(得分:0)

尝试这种方式:

radioButton = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());

对于已检查状态更改的每个radiogroup,请将其radiobutton点击