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

Adding Actions

To demonstrate actions we're going to add the capability to manage a list of checked items to CheckboxCursorAdapter. Actions are handled with listeners, we're going to use an onClickListener. The onClickListener should be attached to the checkbox in getView(). All that's needed is creating a creating an ArrayList to hold the checked list, create the onClickListener() to manage the list, and set the list position into the checkbox tag field for use in the list.

The list needs a unique ID for each element in the list that we can store in the ArrayList to manage what is checked. To do this we store the list position into the checkbox tag with setTag(). In getView() create and attach an onClickListener() to the checkbox element, cBox. The onClickListener() expects a class that contains public void method onClick(). The onClickListener() will pass the View element that was clicked into the onClick() method. The onClick() function needs to get the value from the checkbox tag with getTag(). Then if the checkbox was clicked add the position stored in the tag into the ArrayList. And if the checkbox was unchecked remove the tag from the ArrayList.

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<String> checkList = new ArrayList<String>();
	
	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(pos);
		cBox.setText(bookmark);
		if (Integer.parseInt(this.c.getString(this.c.getColumnIndex(Browser.BookmarkColumns._ID))) % 2 != 0) {
			cBox.setChecked(true);	
			checkList.add((String) cBox.getTag());
		}
		cBox.setOnClickListener(new OnClickListener() {  
			@Override
			public void onClick(View v) {
				CheckBox cBox = (CheckBox) v.findViewById(R.id.bcheck);
				if (cBox.isChecked()) {
					//cBox.setChecked(false);
					checkList.add((String) cBox.getTag());
				} 
				else if (!cBox.isChecked()) {
					//cBox.setChecked(true);
					checkList.remove(cBox.getTag());
				}
			}
		});

		return(v);
	}

}

No other changes are required other than the additions to CheckboxCursorAdapter to enable the onClickListener. This should be enough to get you started building and presenting lists in Android applications.

Checkboxed Lists <<  1 2 3 4 5
New Content