Checkbox Text List :: Extension of Iconified Text tutorial
What you will learn:How to create a list of items with Checkboxes
Difficulty:2.0 / 5
What it will look like:
Description:
This tutorial grew from another tutorial here which dealt with making lists with icons attached to each list item. (IconifiedTextView tutorial).
1.In order to make a List which includes checkboxes, we need to modify a few things. InCheckBoxifiedText.java, we made a String to hold the text of our list item. We also need a boolean value to keep track of the status of the checkbox (checked = true, unchecked = false). The constructor will initialize the checkbox to be checked or unchecked.
Java: |
public
classCheckBoxifiedText
implementsComparable
{
private StringmText = ""; private booleanmChecked; publicCheckBoxifiedText ( Stringtext, booleanchecked ) { /* constructor */ mText = text; mChecked = checked; } |
2.Second, we need to look atCheckboxifiedTextViewAdapter.java. We need to add code to support our checkbox.(src.getChecked()will tell us whether our box is checked or not.
Java: |
public
ViewgetView
(
intposition,
ViewconvertView, ViewGroup parent
)
{
CheckBoxifiedTextView btv; if (convertView == null ) { btv = newCheckBoxifiedTextView (mContext, mItems. get (position ) ); } else { // Reuse/Overwrite the View passed // We are assuming(!) that it is castable! CheckBoxifiedText src = mItems. get (position ); btv = (CheckBoxifiedTextView )convertView; btv. setCheckBoxState (src. getChecked ( ) ); btv = (CheckBoxifiedTextView )convertView; btv. setText (mItems. get (position ). getText ( ) ); } returnbtv; } |
We also want to add some methods for doing things like getting the state of the checkbox, or selecting all the items.
Java: |
public
voidselectAll
(
)
{
for (CheckBoxifiedText cboxtxt: mItems ) cboxtxt. setChecked ( true ); /* Things have changed, do a redraw. */ this. notifyDataSetInvalidated ( ); } |
3.Open upCheckBoxifiedTextView.java. We need to set up where we want the checkbox to be located, the text location, and whether the box is checked or not.
Java: |
publicCheckBoxifiedTextView
(
Contextcontext, CheckBoxifiedText aCheckBoxifiedText
)
{
super (context ); /* First CheckBox and the Text to the right (horizontal), * not above and below (vertical) */ this. setOrientation (HORIZONTAL ); mCheckBoxText = aCheckBoxifiedText; mCheckBox = newCheckBox (context ); mCheckBox. setPadding ( 0, 0, 20, 0 ); // 5px to the right /* Set the initial state of the checkbox. */ mCheckBox. setChecked (aCheckBoxifiedText. getChecked ( ) ); /* At first, add the CheckBox to ourself * (! we are extending LinearLayout) */ addView (mCheckBox, newLinearLayout. LayoutParams ( LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT ) ); mText = newTextView (context ); mText. setText (aCheckBoxifiedText. getText ( ) ); //mText.setPadding(0, 0, 15, 0); addView (mText, newLinearLayout. LayoutParams ( LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT ) ); } |
4.Finally, we can look at our ListActivity code which will be using the classes we just made. In the attached source code, this is theCheckbox.javafile. For this example, i am using an array of strings which contain the text we want in each list item. We create a checkboxlist adapter, cbla, first.
Then, we loop through each item in the array and add it to the list adapter (usingcbla.addItem()). I am setting all the checkboxes to initially be unchecked by passing the value of FALSE.
Java: |
public
class
Checkbox
extendsListActivity
{
/** Called when the activity is first created. */ privateCheckBoxifiedTextListAdapter cbla; // Create CheckBox List Adapter, cbla private String [ ]items = { "Box 1", "Box 2", "Box 3", "Box 4" }; // Array of string we want to display in our list @Override public voidonCreate (Bundle icicle ) { super. onCreate (icicle ); setContentView (R. layout. main ); cbla = newCheckBoxifiedTextListAdapter ( this ); for ( intk= 0; k length; k++ ) { cbla. addItem ( newCheckBoxifiedText (items [k ], false ) ); } // Display it setListAdapter (cbla ); } } |
Thats pretty much it. Not much work! Attached are all the source files you will need for this tutorial. I have also added functions such as select all, and deselect all which will come in handy.