2013-06-26 77 views
1

我有一個列表視圖(它充滿布局)和一個自定義適配器擴展BaseAdapter。 listview的每一行都包含一個單選按鈕,一個textview和一個imageview。從充氣列表視圖佈局添加RadioButtons到RadioGroup

我的問題是我無法將所有單選按鈕添加到RadioGroup中。我想控制單選按鈕,以便在任何時候只能選擇一個。

有人可以幫忙嗎?

我的列表視圖的佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<ListView 
    android:id="@+id/lv_activity_object_setup_final_objects" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</ListView> 

我的XML用於膨脹:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<RadioButton 
    android:id="@+id/rbn_activity_object_setup_final_list_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_marginLeft="22dp" 
    android:layout_marginTop="22dp" 
    android:text="" android:button="@drawable/radio_button"/> 

<ImageView 
    android:id="@+id/img_activity_object_setup_final_list_layout_div_vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="22dp" 
    android:layout_marginTop="5dp" 
    android:layout_toRightOf="@id/rbn_activity_object_setup_final_list_layout" 
    android:src="@drawable/table_divider_vertical" /> 

<TextView 
    android:id="@+id/txv_activity_object_setup_final_list_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="16dp" 
    android:layout_marginTop="22dp" 
    android:layout_toRightOf="@id/img_activity_object_setup_final_list_layout_div_vertical" 
    android:text="Object" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<ImageView 
    android:id="@+id/img_activity_object_setup_final_list_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_marginRight="16dp" 
    android:src="@drawable/object_icon_keys" /> 

我定製適配器代碼:

class ObjectAdapter extends BaseAdapter { 
    Context context; 
    List<ObjectType> objectTypeData; 
    LayoutInflater inflater; 

    public ObjectAdapter(Context context, List<ObjectType> objectTypeList) { 
     this.context = context; 
     inflater = LayoutInflater.from(context); 
     this.objectTypeData = objectTypeList; 
    } 

    @Override 
    public int getCount() { 
     return objectTypeData.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return objectTypeData.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewGroup vg; 

     if (convertView != null) { 
      vg = (ViewGroup) convertView; 
     } else { 
      vg = (ViewGroup) inflater.inflate(R.layout.activity_object_setup_final_list_layout, null); 
     } 

     ObjectType objectType = objectTypeData.get(position); 

     RadioButton radioButton = (RadioButton) vg.findViewById(R.id.rbn_activity_object_setup_final_list_layout); 
     radioButton.setButtonDrawable(getResources().getDrawable(R.drawable.radio_button)); 
     if (objectType.getRadioButtonImage() == strDisabled) { 
      radioButton.setEnabled(false); 
      radioButton.setChecked(true); 
     } 
     else if (objectType.getRadioButtonImage() == strChecked) { 
      radioButton.setEnabled(true); 
      radioButton.setChecked(true); 
     } 
     else { 
      radioButton.setEnabled(true); 
      radioButton.setChecked(false); 
     } 

     TextView objectName = (TextView) vg.findViewById(R.id.txv_activity_object_setup_final_list_layout); 
     objectName.setText(objectType.getTitle()); 

     ImageView objectImage = (ImageView) vg.findViewById(R.id.img_activity_object_setup_final_list_layout); 
     if (objectType.getObjectTypeImage() == getString(R.string.strKeys)) { 
      objectImage.setImageDrawable(getResources().getDrawable(R.drawable.object_icon_keys)); 
     } 
     else if (objectType.getObjectTypeImage() == getString(R.string.strWallet)) { 
      objectImage.setImageDrawable(getResources().getDrawable(R.drawable.object_icon_wallet)); 
     } 
     else if (objectType.getObjectTypeImage() == getString(R.string.strBag)) { 
      objectImage.setImageDrawable(getResources().getDrawable(R.drawable.object_icon_bag)); 
     } 
     else if (objectType.getObjectTypeImage() == getString(R.string.strLaptop)) { 
      objectImage.setImageDrawable(getResources().getDrawable(R.drawable.object_icon_laptop)); 
     } 
     else { 
      objectImage.setImageDrawable(getResources().getDrawable(R.drawable.object_icon_default)); 
     } 

     return vg; 
    } 
} 
+0

爲什麼你需要RadioGroup中?您是否試圖從您的適配器控制單一檢查? ObjectType包含哪些數據? – sandrstar

+0

哦,我想讓自動控制,以便我不必編碼檢查。 ObjectType是他的modaldata圖層,其中包含3項: \t public String radioButtonImage; \t public String title; \t public String objectTypeImage; 有關如何在適配器中對其進行編碼的任何線索? – Terence

+0

您可以嘗試跟蹤當前選中的項目(例如其索引項目),並在選擇其他項目時更新它。在適配器中您需要檢查位置== checkedIndex並相應地設置項目狀態。 – sandrstar

回答

2
  1. 將單選按鈕添加到OnClickListener。
  2. 在OnClick事件中,保存selectedItem索引。
  3. 調用適配器的notifyDataSetChanged方法。
  4. 在適配器中,檢查selectedItem ==位置是否決定是否檢查單選按鈕。

radioButton.setChecked(selectionIndex==position);