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"
/>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to