Hi,I am new to mobile and i am still studying the components,layouts,etc..first i want to know how can i put radiogroup in listview,
i made another xml file "Radiolayout.xml" but i could not display this in listview.

More inputs will be much more appreciated.

This is my java file "Sample01.java"

package com.Android.Sample01;


import android.os.Bundle;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.RadioGroup;
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 Sample01 extends ListActivity
   {
      public void onCreate(Bundle savedInstanceState) 
       {
         super.onCreate(savedInstanceState);

         final String[] str = new  String []{"listview1","listview2"};
         setListAdapter(new ArrayAdapter<String>(this,R.layout.Mylayout,
                        str));

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

         lview.setOnItemClickListener(new OnItemClickListener()
         {

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

             }

         });




      }
   }

XML FILE. "Mylayout.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" >

</TextView>

Main.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

</LinearLayout>

Radiolayout.xml

<?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"
            android:text="ButtonOne " />

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


    </RadioGroup>


</LinearLayout>

You can not have two radio groups on same listview. You have to provide separate listview for each group

Hi,peter budo,can you please guide me on your steps separating listiview in each group.
this what i understand,please correct me if i am wrong.
i make another "Mradiogroup.java"

package com.Android.Sample01;


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 Mradiogroup extends ListActivity
   {


      public void onCreate(Bundle savedInstanceState) 
       {
         super.onCreate(savedInstanceState);
         final String[] str1 = new  String []{"radio1","radio2"};
         setListAdapter(new ArrayAdapter<String>(this,R.layout.radiolayout,str1));
         ListView lview1 = getListView();
         lview1.setTextFilterEnabled(true);

         lview1.setOnItemClickListener(new OnItemClickListener()
         { 
             public void onItemClick(AdapterView<?> parent,View eview1,int position,long id)
             {
               Toast.makeText(getApplicationContext(),((TextView)eview1).getText(),
               Toast.LENGTH_SHORT).show();
             }

         });     
       }
   }

and this is my radiolayout.xml

<?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>
    </RadioGroup>
</LinearLayout>

You would have to explain more in details what you trying to achieve because you can provide list view through Spinners for example like this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
                android:id="@+id/vegetables"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Vegetables"
                />
        <Spinner
            android:id="@+id/vegetables_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/vegetables"
            />
        </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
                android:id="@+id/fruits"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Fruits"
                />
        <Spinner
            android:id="@+id/fruits_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/fruits"
            />
        </RelativeLayout>
</LinearLayout>






package com.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class ListTestActivity extends Activity {

    private String[] vegetables = new String[] {"potatoes", "onion", "carrot"};
    private String[] fruits = new String[] {"apple", "pear", "strawberry", "banana"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner vegSpinner = (Spinner) findViewById(R.id.vegetables_spinner);
        ArrayAdapter<String> vegAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, vegetables);
        vegAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        vegSpinner.setAdapter(vegAdapter);

        Spinner fruitSpinner = (Spinner) findViewById(R.id.fruits_spinner);
        ArrayAdapter<String> fruitAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, fruits);
        fruitAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        fruitSpinner.setAdapter(fruitAdapter);
    }

}

Hi peter budo,what is that spinner?

Hi peter budo,i want to have radiogroup in my listview how can i achieve this,may you can help me thank you.

You can not have two radio groups on same listview. You have to provide separate listview for each group

@peter budo How can i put radiogroup in listview thats what i want?

Sorry for late reply long weekend in UK.

Each ListView is a group in its own. Unlike radio buttons attached to other layouts and views you do not need to set RadioGroup on ListView

As for Spinner, it is adaptation of drop down menu as you know it from web site or from Java as JComboBox. So it is sort of embeded ListView.

@peter budo,correct me if i am wrong you mean that radiogroup cannot be put in listview?only the radiobuttons, so how can i do this in listview with radio buttons.?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.