0

RadioButtonの中に入れたかったListViewのですが、問題は、にRadioButton入力されたすべてのものが選択できるためListView、それぞれが異なることです。RadioGroupアイテムを 1 つだけ選択し、その位置の checkFlag を true に設定し、他の位置を false に設定します。RadioButtonそれぞれを使用して追加しましaddViewたが、トリガーされましたjava.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

これが私のものgetViewです:

@Override
public View getView(final int position, View convertView,
        ViewGroup parent) {
    View row = convertView;
    holder = null;
    final Option optionItem = data[position];

    protected int selectedPosition = -1;
    protected boolean selectedCheckFlag = false;

    if (row == null) {
        LayoutInflater inflater = ((Activity) context)
                .getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new OptionHolder();

        holder.radioGroup = (RadioGroup) row
                .findViewById(R.id.radiogroup_survey);
        holder.radioButton = (RadioButton) row
                .findViewById(R.id.radiobutton_survey);
        row.setTag(holder);

    } else {
        holder = (OptionHolder) row.getTag();
    }

    holder.radioGroup.setOrientation(RadioGroup.VERTICAL);

    holder.radioButton.setText(optionItem.option);
    holder.radioGroup.addView(holder.radioButton); // Triggered IllegalStateException

    holder.radioGroup
            .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group,
                        int checkedId) {
                    // TODO Auto-generated method stub
                    RadioButton selectedRadio = (RadioButton) findViewById(checkedId);
                    Toast.makeText(getApplicationContext(), selectedRadio.getText().toString(), Toast.LENGTH_SHORT).show();
                    optionItem.

                    selectedCheckFlag = true;
                                selectedPosition = position;

                                if (selectedPosition > -1
                                        && selectedCheckFlag)
                                    optionItem.checkFlag = true;
                                else
                                    optionItem.checkFlag = false;
                }
            });

    return row;
}

アイテムのレイアウトは次のとおりです。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent_radio"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RadioGroup
    android:id="@+id/radiogroup_survey"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radiobutton_survey"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:button="@drawable/selector_checkbox_red"
        android:textColor="@color/brown" />
</RadioGroup>

4

1 に答える 1

0

xml を使用して既に追加RadioButtonしているため、同じボタンを再度追加する必要はありません。RadioGroupコメントaddView行:

// holder.radioGroup.addView(holder.radioButton)

問題は、ListView に取り込まれたすべての RadioButton が異なる RadioGroup を持つことです。これは、それらすべてを選択できるためです。アイテムを 1 つだけ選択し、その位置の checkFlag を true に設定し、他の位置を false に設定したい

のすべてのラジオボタンのチェックを外すには、次のRadioGroup呼び出しclearCheck()方法を使用しRadioGroupます。

holder.radioGroup.clearCheck();
于 2014-03-03T04:15:37.490 に答える