1

How to use checkbox with listview .in this i want to perform some task on checkbox click .I didnt get get how can i pick that particular id or listitem value , using checkbox.

Mapoo
  • 41
  • 4
  • 1
    post your code. what have your tried so far.. this is very basic question in android. you will get so many answer in google search... – user1835052 Apr 11 '13 at 04:23

3 Answers3

1

Just override getview method of your array adapter. And use viewholder to get your checkbox of customlist.

example code:

public View getView(int position, View convertView, ViewGroup parent) {

   ViewHolder holder = null;
   Log.v("ConvertView", String.valueOf(position));

   if (convertView == null) {
   LayoutInflater vi = (LayoutInflater)getSystemService(
     Context.LAYOUT_INFLATER_SERVICE);
   convertView = vi.inflate(R.layout.your_layout, null);

   holder = new ViewHolder();      
   holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
   convertView.setTag(holder);

    holder.name.setOnClickListener( new View.OnClickListener() { 
     public void onClick(View v) { 
      CheckBox cb = (CheckBox) v ; 

      Toast.makeText(getApplicationContext(),
       "Clicked on Checkbox: " + cb.getText() +
       " is " + cb.isChecked(),
       Toast.LENGTH_LONG).show();

     } 
    }); 
   } 

for brief tutorial ref this link.

I hope this will help you.

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
1

Try this, List Adapter:-

public class CheckBoxInListAdapter extends BaseAdapter{
    private Context context=null;
    private List<ArrayListObjects> searchArrayList=null;
    private LayoutInflater mInflater=null;
    private ViewHolder holder=new ViewHolder();

    public CheckBoxInListAdapter(Context context, List<ArrayListObjects> mData) 
    {
        this.context=context;
        searchArrayList = mData;
        mInflater = LayoutInflater.from(context);
    }
    public int getCount() 
    {
        return searchArrayList.size();
    }
    public Object getItem(int position) 
    {
        return searchArrayList.get(position);
    }
    public long getItemId(int position) 
    {
        return position;
    }
    public View getView(final int position, View convertView, ViewGroup parent)
    {
         if (convertView == null) 
         {
              convertView = mInflater.inflate(R.layout.sample_layout,null);
              holder=new ViewHolder();
              holder.textView = (TextView) convertView.findViewById(R.id.textView);
              holder.itemNormalChecked=(CheckBox)convertView.findViewById(R.id.checkBox);
              convertView.setTag(holder);
         } 
         else 
         {
             holder = (ViewHolder) convertView.getTag();
         }
         holder.textView.setText(searchArrayList.get(position).getItemName());
         holder.itemNormalChecked.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if(searchArrayList.get(position).isItemChecked())
                {
                    searchArrayList.get(position).setItemChecked(false);
                }
                else
                {
                    searchArrayList.get(position).setItemChecked(true);
                }
            }
         });
         if(searchArrayList.get(position).isItemChecked())
         {
             holder.itemNormalChecked.setChecked(true);
         }
         else
         {
             holder.itemNormalChecked.setChecked(false);
         }
         return convertView;
     }
     static class ViewHolder
     {
         TextView textView=null;
         CheckBox itemNormalChecked=null;
     }
}

Layout:- sample_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView 
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/checkBox"
        android:layout_height="wrap_content"/>
    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_height="wrap_content"/>
</RelativeLayout>

And ArrayListObjects.java

public class ArrayListObjects {
    private boolean isItemChecked=false;
    private String itemName="null";
    public boolean isItemChecked() {
        return isItemChecked;
    }
    public void setItemChecked(boolean isItemChecked) {
        this.isItemChecked = isItemChecked;
    }
    public String getItemName() {
        return itemName;
    }
    public void setItemName(String itemName) {
        this.itemName = itemName;
    }
}

then in your activity,

ArrayList<ArrayListObjects> arrayListObjects=new ArrayList<ArrayListObjects>();
for(int i=0;i<15;i++)
{
    arrayListObjects.add((new ArrayListObjects()).setItemName("value -"+String.valueOf(position);
}
listView.setAdapter(new CheckBoxInListAdapter(activityName.this,arrayListObjects);

After you want to get checked values from listview then,

for(int i=0;i<listView.getCount();i++)
{
    ArrayListObjects obj=(ArrayListObjects)listView.getItemAtPosition(i);
    if(obj.isItemChecked())
    {   
       Log.e("checked items - ",obj.getItemName());
    }
}
Sino Raj
  • 6,431
  • 2
  • 22
  • 23
0

This can be done by using the using CompoundButton.OnCheckChangedListener. You can set this listener inline like this:

//Create a CheckBox object from your CheckBox id that is in the ListView in your xml layout file
CheckBox foo = (CheckBox) findViewById(R.id.foo);

foo.setOnCheckedChangedListener( new CompoundButton.OnCheckedChangedListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked = true){
            // perform some operations when checked
        } else {
            // perform some operations when unchecked
    }

});

Documentation on Android Checkboxes and CompoundButtons(The CheckBox superclass) can be found here:

The Android documentation is extremely useful, it can sometimes be hard to cipher through it and find what you want. Hope this answers your questions!

alice.harrison
  • 80
  • 1
  • 12