Select only one radion button from listview (Example with source code)

I got this same problem few days ago and I searched it over and over but I did not come out with a perfect solution. And I know that many guys already got this problem. Here is the step by step procedure that will show you how to implement this scenario for android.
I think you have basic idea to implement Listview in android. For this project we have to use two xml templates one will hold the listview and another will hold the listview template(where we can put the radio button). We can name one template main_listview.xml and another template item_listview.xml. Lets see how I created this. 

First of all create a project and name it RadioListSingle and create two xml layout. One for main_listview.xml and another one for item_listview.xml. 
The main_listview.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <ListView

        android:id="@+id/id_listview"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent" >

    </ListView>

</LinearLayout>



Important: we use a tricks in ListView height. Whatever the parent height is but you have to put ListView height fill_parent otherwise it will not works perfectly.
Now the item_listview.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rg_payment_method"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
        <RadioButton
            android:id="@+id/rdb_payment_method"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:checked="false"
            android:drawablePadding="10dp"
            android:text="Visa x-1234" />
        <Button
            android:id="@+id/bt_item_event"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_marginTop="12dp"
            android:text="Add Radio" />
</LinearLayout>
Our xml file is completed now we have to create our listview. At first create a data provider class for each list. We can name it DataList.class.
The Data Provider Class:
package com.example.radiolistsingle;

public class DataList {
    private String rdText;
    private String btText;

    public DataList(String rdText, String btText) {
        super();
        this.rdText = rdText;
        this.btText = btText;
    }

    public String getRdText() {
        return rdText;
    }

    public void setRdText(String rdText) {
        this.rdText = rdText;
    }

    public String getBtText() {
        return btText;
    }

    public void setBtText(String btText) {
        this.btText = btText;
    }

}
Now it is time to write the listview code. Here is the code snippet for you.
The AdapterList class:
package com.example.radiolistsingle;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;

public class AdapterList extends ArrayAdapter<DataList> {

    Context context;
    ViewHolder holder;
    private boolean userSelected = false;
    private RadioButton mCurrentlyCheckedRB;

    public AdapterList(Context context, int textViewResourceId,
            List<DataList> items) {
        super(context, textViewResourceId, items);
        this.context = context;
    }

    private class ViewHolder {
        RadioButton radioBtn;
        Button btAdd;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        holder = null;
        DataList rowItem = getItem(position);
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_listview, null);
            holder = new ViewHolder();
            holder.radioBtn = (RadioButton) convertView
                    .findViewById(R.id.rdb_id);
            holder.btAdd = (Button) convertView
                    .findViewById(R.id.bt_item_event);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        if (position == getCount() - 1 && userSelected == false) {
            holder.radioBtn.setChecked(true);
            mCurrentlyCheckedRB = holder.radioBtn;
        } else {
            holder.radioBtn.setChecked(false);
        }

        holder.radioBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (mCurrentlyCheckedRB != null) {
                    if (mCurrentlyCheckedRB == null)
                        mCurrentlyCheckedRB = (RadioButton) v;
                    mCurrentlyCheckedRB.setChecked(true);
                }

                if (mCurrentlyCheckedRB == v)
                    return;

                mCurrentlyCheckedRB.setChecked(false);
                ((RadioButton) v).setChecked(true);
                mCurrentlyCheckedRB = (RadioButton) v;
            }
        });

        holder.radioBtn.setText(rowItem.getRdText());
        holder.btAdd.setText(rowItem.getBtText());
        return convertView;
    }

}
We have done our first step and the second step is to check it by creating a main class. In the main class we have to create instance of adapter class. Here is the code.
The main Class:
package com.example.radiolistsingle;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainListviewActivity extends Activity {
   
    private ListView listView;
    private List<DataList> rowItems;
    DataList listData;
    AdapterList adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_listview);
      
        rowItems = new ArrayList<DataList>();

        listData = new DataList("Radio One", "Add Method");
        rowItems.add(listData);
        listData = new DataList("Radio Two", "Add Method");
        rowItems.add(listData);

        listView = (ListView) findViewById(R.id.id_listview);
        adapter=new AdapterList(this, R.layout.item_listview, rowItems);
        listView.setAdapter(adapter);
    }
}
You can download the full code from GitHub. I will upload it immediately.
Just now run the application. Hope it will works fine and if you have any problem with this code, you can ask by comment.

Comments

Popular posts from this blog

How to show only month and year fields in android Date-picker?

How to construct a B+ tree with example

Conflict Serializability in database