【问题标题】:RadioButton inside ListView gives IllegalStateExceptionListView 内的 RadioButton 给出 IllegalStateException
【发布时间】:2014-04-04 00:18:52
【问题描述】:

我想在ListView 中包含RadioButton,问题是ListView 中填充的每个RadioButton 都有不同的RadioGroup,因为它们都可以选择。我只想选择一个项目并将其位置的 checkFlag 设置为 true,将其他位置设置为 false。我使用addView 添加了每个RadioButton,但触发了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>

【问题讨论】:

  • 在此处发布您的 logcat 详细信息
  • 但是您已经在 xml 中添加了 RadioButton 那么为什么要再次尝试添加相同的 Button 呢?只需评论holder.radioGroup.addView(holder.radioButton)

标签: android android-listview illegalstateexception android-radiogroup android-radiobutton


【解决方案1】:

因为您已经使用 xml 在RadioGroup 中添加了RadioButton,所以无需再次添加相同的按钮。评论addView线:

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

问题是 ListView 中填充的每个 RadioButton 都有 不同的 RadioGroup,因为它们都可以选择。我只想 要选择的一项并将其位置的 checkFlag 设置为 true 和 其他位置为假

要取消选中来自RadioGroup 的所有单选按钮,请使用RadioGroup 的调用clearCheck() 方法:

holder.radioGroup.clearCheck();

【讨论】:

  • 如果我评论那一行,我不能使用holder.radioGroup.setOnCheckedChangeListener,其中我将optionItem.checkFlag 设置为true。由于我必须保存在 POJO Option 中以供进一步使用。
  • @CompaqLE2202x : 也使用holder.radioButton.getText() 而不是RadioButton selectedRadio = (RadioButton) findViewById(checkedId); 从选定的单选按钮获取文本
猜你喜欢
  • 1970-01-01
  • 2012-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-17
  • 1970-01-01
  • 2021-07-21
相关资源
最近更新 更多