2010-12-03 49 views
6

我有動態生成的ListView,它包含單選按鈕作爲列表項。Android。在ListView中的RadioButtons

是否有可能在該列表視圖或這些單選按鈕中使用radiogroup功能。

我的意思是,如果用戶選擇一個單選按鈕,之前選擇的單選按鈕將被取消選中。

這是我目前的解決方案,我不太喜歡。我只保存選定的單選按鈕,如果選擇另一個,則取消選擇已保存的單選按鈕。

感謝您的建議或鏈接。

這裏是我的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:background="@drawable/bg_tile" 
android:padding="10dp"> 
<TextView 
    android:id="@+id/text_station_name" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="5dp" 
    android:text="TEST" 
    android:textColor="@color/black" 
    android:background="@color/transparent_white"/> 
<ListView 
    android:id="@+id/list_lines" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/>  
</LinearLayout> 

item.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/rb_lineId"  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:textColor="@color/black" 
android:paddingLeft="50dp" 
android:background="@drawable/selector_custombutton"/> 

但我也試圖與這一個:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:background="@drawable/bg_tile" 
android:padding="10dp"> 
<TextView 
    android:id="@+id/text_station_name" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="5dp" 
    android:text="TEST" 
    android:textColor="@color/black" 
    android:background="@color/transparent_white"/> 
    <RadioGroup 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
    <ListView 
    android:id="@+id/list_lines" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 
</RadioGroup>  
</LinearLayout> 
+0

可能的重複http://stackoverflow.com/questions/4250599/android-listview-with-radiobutton-in-singlechoice-mode-and-a-custom-row-layout – 2011-11-04 05:55:50

+0

檢查出ItemRenderer ..設置項目renderer在列表中的任何對象yu要在列表中顯示... rajan ... – Rajan 2010-12-03 09:26:49

回答