簡體   English   中英

如何從Android中動態創建的多個單選組中獲取正確的單選按鈕索引

[英]How to fetch correct radio button index from multiple radio groups created dynamically in 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。 兩個無線電組的2個偵聽器如下:

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嗎? 我到底在做什么錯?

音頻文件

請幫忙 ! 先感謝您!

以這種方式嘗試:

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

對於radiogroup檢查狀態更改的每個radiogroup ,放置它並獲得單擊的radiogroup radiobutton

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM