Skip to content Skip to sidebar Skip to footer

Custom Listview With Checkbox Single Selection

Here I am creating custom listview with checkbox / RadioButton. I got this but I need the single selection for that. I try using this lstvw.setChoiceMode(ListView.CHOICE_MODE_SINGL

Solution 1:

Here is what i would do if i need to select only single item at a time.

Home.java (Activity)

package com.lvcheck.activities;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

publicclassHomeextendsActivity 
{
    privateListView lvCheckBox;
    privateButton btnCheckAll, btnClearALl;
    privateString[] arr = {"One", "Two", "Three", "Four", "Five", "Six"};

    @OverridepublicvoidonCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnCheckAll = (Button)findViewById(R.id.btnCheckAll);
        btnClearALl = (Button)findViewById(R.id.btnClearAll);

        lvCheckBox = (ListView)findViewById(R.id.lvCheckBox);
        lvCheckBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lvCheckBox.setAdapter(newArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, arr));


        btnCheckAll.setOnClickListener(newOnClickListener() 
        {           
            @OverridepublicvoidonClick(View arg0) 
            {
                for(int i=0 ; i < lvCheckBox.getAdapter().getCount(); i++)
                {
                    lvCheckBox.setItemChecked(i, true);             
                }
            }
        });

        btnClearALl.setOnClickListener(newOnClickListener() 
        {           
            @OverridepublicvoidonClick(View v) 
            {
                for(int i=0 ; i < lvCheckBox.getAdapter().getCount(); i++)
                {
                    lvCheckBox.setItemChecked(i, false);                
                }
            }
        });
    }
}

and my (main.xml) XML file should like this

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayoutandroid:layout_width="fill_parent"android:orientation="horizontal"android:gravity="center"android:layout_height="wrap_content"><Buttonandroid:layout_width="wrap_content"android:text="Check All"android:layout_marginRight="7dip"android:id="@+id/btnCheckAll"android:layout_height="wrap_content"></Button><Buttonandroid:layout_width="wrap_content"android:text="Clear All"android:id="@+id/btnClearAll"android:layout_height="wrap_content"></Button></LinearLayout><ListViewandroid:layout_width="fill_parent"android:id="@+id/lvCheckBox"android:fadingEdge="none"android:cacheColorHint="@null"android:layout_height="fill_parent"></ListView></LinearLayout>

so the output will be like this way..

enter image description here

Source: here

let me know if you have any trouble regarding this.

Edit: check this useful link: Custom Single choice ListView

Solution 2:

You can use CheckBox in your adapter class. Try this.

@Overridepublic View getView(finalint position, View convertView,
            ViewGroup parent) {
        // TODO Auto-generated method stubif (convertView == null) {
            inflater = LayoutInflater.from(adapterContext);
            convertView = inflater.inflate(R.layout.view, null);
            finalViewHolderviewHolder=newViewHolder();

            viewHolder.name = (TextView) convertView.findViewById(R.id.txtName);
            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);

            convertView.setTag(viewHolder);
        }

        finalViewHolderholder= (ViewHolder) convertView.getTag();

        holder.name.setText(collectContactList.get(position).getName());

        holder.checkBox.setOnClickListener(newOnClickListener() {

            @OverridepublicvoidonClick(View v) {
                // TODO Auto-generated method stubCheckBoxcb= (CheckBox) v;
                if (cb.isChecked() == true) 
                {
                    // Here you will get list of checked item.
                }

                else 
               {
                // Here you will get list of unchecked item.    
                }

            }
        });

Hope this will help you.

Post a Comment for "Custom Listview With Checkbox Single Selection"