Android Tutorials
Working With Android Contacts
Accessing Data With Android Cursors
Creating Lists Using The Android ListActivity
Android 9 Patch Scaled PNG Image Guide
Working With Images In Android
Exploring Android LinearLayout And RelativeLayout
Writing A Basic Android Application
Installing The Android SDK In Eclipse

Creating Lists Using The Android ListActivity

Using Checkboxes

Creating checked lists is similar to creating the image list on the previous page. A custom layout will need to be created and the SimpleCursorAdapter class will need to be extended to override getView(). Create a new xml file in res/layout/ called check_list.xml. This will hold the checked list layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
  <CheckBox
       android:id="@+id/bcheck"
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
       />
</LinearLayout>

This is a simple LinearLayout although the only component is the CheckBox element with the id of bcheck. CheckBox elements are really a view themselves. They consist of the checkbox and a TextView component to the right. This makes using checkboxes very easy as the code below will show.

Create a new class in the project called CheckboxCursorAdapter that extends android.widget.SimpleCursorAdapter.

package com.higherpass.android.widgets.test; 

import java.util.ArrayList; 

import android.content.Context;
import android.database.Cursor;
import android.provider.Browser;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class CheckboxCursorAdapter extends SimpleCursorAdapter {

	private Cursor c;
	private Context context;
	private ArrayList<Integer> checkList = new ArrayList<Integer>();
	
	public CheckboxCursorAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to) {
		super(context, layout, c, from, to);
		this.c = c;
	        this.context = context;
	}
	
	public View getView(int pos, View inView, ViewGroup parent) {
		View v = inView;
		if (v == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.check_list, null);
		}
                this.c.moveToPosition(pos);		
		String bookmark = this.c.getString(this.c.getColumnIndex(Browser.BookmarkColumns.TITLE));
		CheckBox cBox = (CheckBox) v.findViewById(R.id.bcheck);
		cBox.setTag(Integer.parseInt(this.c.getString(this.c.getColumnIndex(Browser.BookmarkColumns._ID))));
		cBox.setText(bookmark);
		if (Integer.parseInt(this.c.getString(this.c.getColumnIndex(Browser.BookmarkColumns._ID))) % 2 != 0) {
			cBox.setChecked(true);	
		}
		return(v);
	}

}

The CheckboxCursorAdapter class has a simple constructor just like the ImageCursorAdapter. The constructor just needs to pass the data into the SimpleCursorAdapter constructor. The getView() method is very similar to before as well. If the passed view is null we need to use the LayoutInflater to create a view from the layout we created in check_list.xml. Then move the cursor to the correct position in the list and populate the data into the correct parts of the view.

Use the cBox.setText() method to set the text of the checkbox. To check the checkbox by default use the setChecked() function to set the checked state. In this example the checkbox defaults as checked to all items with an odd database ID in the list.

A few modifications are needed to TestListActivities to reflect the checked list.

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String[] projection = new String[] {Browser.BookmarkColumns._ID, 
                                    Browser.BookmarkColumns.TITLE};
        String[] displayFields = new String[] {Browser.BookmarkColumns.TITLE};
        int[] displayViews = new int[] {R.id.btitle};
 
        Cursor cur = managedQuery(android.provider.Browser.BOOKMARKS_URI, 
                       projection, null, null, null);
 
        setListAdapter(new CheckboxCursorAdapter(this, 
        		R.layout.check_list, cur, 
			displayFields, displayViews
        ));
    }

Images In Lists <<  1 2 3 4 5  >> Adding Actions To Lists
New Content