Getting the state of a checkbox inside of a listview

Showing 1-6 of 6 messages
Getting the state of a checkbox inside of a listview SnowDrifter 4/3/08 7:57 PM
I have a ListView that contains a list of checkbox and text pairs. I
am having trouble figuring out how to access the data in the checkbox
or get an onclick from the checkbox since there are n of them and I
can't exactly give each one an ID.

This has been posted before but nobody had a solution:
http://groups.google.com/group/android-developers/browse_thread/thread/d32799e6be80b4b2/134c99a940d37910?hl=en#134c99a940d37910

I was hoping there was a fix for this in M5. Anybody know?
Re: [android-developers] Getting the state of a checkbox inside of a listview Pierre Neihouser 4/4/08 12:51 AM
Create your own view (CheckboxView) where you have only a checkbox.
Do the processing in your own CheckboxView, the correct instance will be called each time.
If you get it all working with the new touch mode (and the keys mode), let me know as I've been banging my head on this one for the past 2 weeks.
Re: [android-developers] Re: Getting the state of a checkbox inside of a listview Romain Guy 4/4/08 12:55 AM
It is actually not very difficult to implement a list of checkboxes.
The key is to write a custom Adapter that keeps track of the selected
items (using a SparseBooleanArray for instance.) I wrote such an
adapter as an ApiDemo recently, I'll try to find the code and paste it
here. Note that the next version of the SDK will provide an option for
ListView to automatically behave as a list of check boxes without any
work from your part (it's actually more powerful since it lets you
check an arbitrary view, like an ImageView, a WebView, etc.)

--
Romain Guy
www.curious-creature.org

Re: Getting the state of a checkbox inside of a listview xingye 4/4/08 1:19 AM
here is the demo code what you want.

http://code.google.com/p/sharepath

just checkout the source code, the checkable list in choosebuddy.java.



On 4月4日, 上午10时57分, SnowDrifter <amacp...@gmail.com> wrote:
> I have a ListView that contains a list of checkbox and text pairs. I
> am having trouble figuring out how to access the data in the checkbox
> or get an onclick from the checkbox since there are n of them and I
> can't exactly give each one an ID.
>
> This has been posted before but nobody had a solution:http://groups.google.com/group/android-developers/browse_thread/threa...
Re: [android-developers] Re: Getting the state of a checkbox inside of a listview Romain Guy 4/4/08 9:37 AM
Hi guys,

Here is a sample activity that shows how to create a list of checkboxes:

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.AdapterView;
import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.ViewGroup;

/**
 * This example shows how to create a list of checkboxes.
 */
public class List10 extends ListActivity implements
AdapterView.OnItemClickListener {
    private CheckBoxAdapter mCheckBoxAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mCheckBoxAdapter = new CheckBoxAdapter(this,
R.layout.list_item_checkbox, GENRES);
        setListAdapter(mCheckBoxAdapter);

        final ListView listView = getListView();

        listView.setItemsCanFocus(false);
        listView.setTextFilterEnabled(true);
        listView.setOnItemClickListener(this);
    }

    public void onItemClick(AdapterView parent, View view, int
position, long id) {
        mCheckBoxAdapter.toggle(position);
    }

    private static class CheckBoxAdapter extends ArrayAdapter<String>
            implements CompoundButton.OnCheckedChangeListener {

        private SparseBooleanArray mCheckStates;

        public CheckBoxAdapter(Context context, int resource, String[]
objects) {
            super(context, resource, objects);
            mCheckStates = new SparseBooleanArray(objects.length);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final CheckBox view = (CheckBox) super.getView(position,
convertView, parent);
            view.setTag(position);
            view.setChecked(mCheckStates.get(position, false));
            view.setOnCheckedChangeListener(this);
            return view;
        }

        public boolean isChecked(int position) {
            return mCheckStates.get(position, false);
        }

        public void setChecked(int position, boolean isChecked) {
            mCheckStates.put(position, isChecked);
            notifyDataSetChanged();
        }

        public void toggle(int position) {
            setChecked(position, !isChecked(position));
        }

        public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
            mCheckStates.put((Integer) buttonView.getTag(), isChecked);
        }
    }

    private static final String[] GENRES = new String[] {
        "Action", "Adventure", "Animation", "Children", "Comedy",
"Documentary", "Drama",
        "Foreign", "History", "Independent", "Romance", "Sci-Fi",
"Television", "Thriller"
    };
}

And the XML file for the checkboxes:

<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceLarge"
/>

Re: Getting the state of a checkbox inside of a listview SnowDrifter 4/6/08 9:51 AM
Thanks guys! Both of you have great solutions to this problem. I am
already pulling the data from a database so I may as well use the
SimpleCursorAdapter method that Xingye is using and just hand it my
cursor result and format it how I want. That is one less step to have
to deal with for me.