0

I need to add RadioButton in ListView and indeed I have tried, But the problem that I am getting is that when I click on radio button it get selected wonderfully... but the same time if wish not to select that particular button... it just remains selected. Here I am providing my code:

import android.app.ListActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class RadioInCustomListView extends ListActivity {

    ListView lv;

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


        String[] names = new String[] { "Issues", "Request for Information",
                    "Contracts", "Purchase Orders", "Change Orders", "Proposals",
                    "Submittals" };
            lv = getListView();
            lv.setCacheColorHint(Color.TRANSPARENT);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.radio_item, names);
            //this.setListAdapter (new ArrayAdapter<String>(RadioInCustomListView.this,R.layout.radio_item, names));
            setListAdapter(adapter);
    }

        protected void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);
            Object o = this.getListAdapter().getItem(position);
            Toast.makeText(RadioInCustomListView.this, o.toString(), Toast.LENGTH_LONG).show();


            }
    }

My xmls: main.xml:

<?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"
    >
<ListView  
    android:id="@+android:id/list"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>

radio_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@android:id/text1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:textAppearance="?android:attr/textAppearanceLarge"
  android:gravity="center_vertical"
  android:checkMark="?android:attr/listChoiceIndicatorSingle"
  android:paddingLeft="6dip"
  android:paddingRight="6dip"
/>
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
Jai Android
  • 2,131
  • 10
  • 38
  • 55
  • let check this post. Have similar kind of problem [Click here][1] [1]: http://stackoverflow.com/questions/4250599/android-listview-with-radiobutton-in-singlechoice-mode-and-a-custom-row-layout – sandy Sep 28 '11 at 07:24

1 Answers1

2

may be this code help you

ls.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice,list_sexuality));
ls.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
amity
  • 942
  • 1
  • 8
  • 24
  • 1
    Thanks all for your kind response, Yah I can do that but I need to create my own layout....is there any way to customize the layout(layout.simple_list_item_single_choice) – Jai Android Sep 28 '11 at 07:29