Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android. RadioButtons in a ListView

I have dynamically generated ListView, which consists radio-buttons as list items.

Is it possible to use radiogroup functionality in that listview or for these radiobuttons.

I mean, I'd like, that if the user select a radio button the radio button selected before would deselected.

That is my solution at the moment, which I don't like very much. I just save selected radio button and if another one will be selected, deselect the saved one.

Thank you for your suggestions or links.

Here is my layout:

<?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"/>  

But I also tried with this one:

<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>
like image 718
Tima Avatar asked Dec 03 '10 08:12

Tima


1 Answers

I think you want android:choiceMode="singleChoice". It's a bit tricky to implement (I don't know the exact specifics myself), but it's a starting point.

like image 146
Felix Avatar answered Nov 12 '22 10:11

Felix