1

I need to know how can I put a RadioGroup in a ListView?

I am a beginner at developing android.

What should I do to display the RadioGroup in the ListView.
Please guide me.

This is my code:

enter code here
package com.mySample.Smpl1;


import android.os.Bundle;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView;
import android.app.*;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;

public class Smpl1 extends ListActivity
 {


  public void onCreate(Bundle savedInstanceState) 
   {
     super.onCreate(savedInstanceState);


     final String[] row = new  String []{"row1","row2"};
     setListAdapter(new ArrayAdapter<String>(this,R.layout.radiolayout,
             row));



     ListView lv = getListView();
     lv.setTextFilterEnabled(true);  

     lv.setOnItemClickListener(new OnItemClickListener()
     {

      public void onItemClick(AdapterView<?> parent,View eview,int position,long id)
         {
          Toast.makeText(getApplicationContext(), ((TextView)eview).getText(),
           Toast.LENGTH_SHORT).show();       

         }

     });



   }
}

and this is my rowylayout.xml

       <?xml version="1.0" encoding="UTF-8"?>

      <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/listView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="hahaha">`

     </TextView>



enter code here

my radio.xml

enter code here

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

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:baselineAligned="false" >

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"/>

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"/>
   </RadioGroup>


 </LinearLayout>
Liam
  • 27,717
  • 28
  • 128
  • 190
jemz
  • 4,987
  • 18
  • 62
  • 102
  • 2
    you want to put a radio group in one row or all your rows having single radio button and you want to make them in a radiogroup... – Bharat Sharma May 05 '12 at 08:42
  • @BharatSharma, I want to put radio group in one row in list view,thank you. – jemz May 05 '12 at 14:31

2 Answers2

3
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:baselineAligned="false" >

<RadioButton
    android:id="@+id/radio0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"/>

<RadioButton
    android:id="@+id/radio1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"/>

this code should come inside rowylayout.xml file which is the list item

dinesh sharma
  • 3,312
  • 1
  • 22
  • 32
  • can you show me how can i call that in Class smlp1 because it throws exception,and i could not determine what is the cause for the error. – jemz May 05 '12 at 14:34
  • but removing the TextView tag in rowlayout and replace it with the RadioGroup,The application screen will force to close.it will generate error.can you help me please on this. – jemz May 06 '12 at 14:22
  • use listview and for list view prepare one adapter class that will populate you custom list Item having you radio group for help try this tutorial http://www.vogella.com/articles/AndroidListView/article.html – dinesh sharma May 07 '12 at 12:49
1

try this

   <RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

</RadioGroup>     
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98