0

我希望有RadioButtonListView,問題是每個RadioButton填充在ListView,有不同的RadioGroup,因爲所有的人都可以選擇。我只想選擇一個項目,並將其位置的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.單選裏面ListView控件提供了IllegalStateException異常

這裏是我的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> 

+0

發表您的logcat這裏的細節 –

+0

但你已經在xml中添加了RadioButton,那麼你爲什麼要再次添加相同的Button?只是評論'holder.radioGroup.addView(holder.radioButton)' –

回答

0

因爲你已經在RadioGroup使用XML已經添加RadioButton所以不需要再次添加相同的按鈕。評論addView行:

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

的問題是,每一個單選按鈕在ListView填充,具有 不同RadioGroup中,因爲所有的人都可以選擇。我想只能選擇 一個項目,並設置其位置的checkFlag爲true和其他 位置虛假

RadioGroup使用呼叫clearCheck()方法RadioGroup取消所有單選按鈕:

holder.radioGroup.clearCheck(); 
+0

如果我評論該行,我不能使用'holder.radioGroup.setOnCheckedChangeListener',其中我設置'optionItem.checkFlag'爲true。由於我必須保存在POJO'Option'中才能繼續使用。 –

+0

@ CompaqLE2202x:也使用'holder.radioButton.getText()'而不是'RadioButton selectedRadio =(RadioButton)findViewById(checkedId);'從選定的單選按鈕 –

相關問題