0

I wanted to have RadioButton inside ListView, the problem is that every RadioButton populated in the ListView, has different RadioGroup since all of them can be selected. I want only one item to be selected and set its position's checkFlag to true and other position to false. I added each RadioButton using addView but triggered java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

Here's my 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;
}

Here's the item layout:

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

Compaq LE2202x
  • 2,030
  • 9
  • 45
  • 62

1 Answers1

0

Because you have already added RadioButton in RadioGroup using xml so no need to add same button again. comment addView line:

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

the problem is that every RadioButton populated in the ListView, has different RadioGroup since all of them can be selected. I want only one item to be selected and set its position's checkFlag to true and other position to false

to uncheck all radiobuttons from RadioGroup use call clearCheck() method of RadioGroup :

holder.radioGroup.clearCheck();
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • If I comment that line, I can't use the `holder.radioGroup.setOnCheckedChangeListener` wherein I set `optionItem.checkFlag` to true. Since I have to save in the POJO `Option` for further usage. – Compaq LE2202x Mar 03 '14 at 04:41
  • @CompaqLE2202x : also use `holder.radioButton.getText()` instead of `RadioButton selectedRadio = (RadioButton) findViewById(checkedId);` to get text from selected radiobutton – ρяσѕρєя K Mar 03 '14 at 05:10