简体   繁体   中英

How to fetch correct radio button index from multiple radio groups created dynamically in android

I am creating a quiz android application where in i will be getting the questions and answer's options from the database in an array assign is to a table row with with two radio groups per question as there are four options. It is done in the following manner :

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

    }

When i try to get the index of the selected option..for the first question i am getting proper index..but for all rest of the questions the index is coming as -1. The 2 listeners for both radio groups is as follows :

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


        }
    }
};

Can anybody please suggest how can i fetch correct radio button ids? What exactly am i doing wrong?

音频文件

Please help ! Thank you in advance!

Try in this way:

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

for each radiogroup on checked state change place it and obtain the radiobutton clicked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM