Checkbox Text List

Checkbox Text List :: Extension of Iconified Text tutorial



What you will learn:How to create a list of items with Checkboxes
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 class CheckBoxifiedText implements Comparable {

private String mText = "";
private boolean mChecked;
public CheckBoxifiedText ( String text, boolean checked ) {
/* 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 View getView ( int position, View convertView, ViewGroup parent ) {
CheckBoxifiedTextView btv;
if (convertView == null ) {
btv = new CheckBoxifiedTextView (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 ( ) );
}
return btv;
}


We also want to add some methods for doing things like getting the state of the checkbox, or selecting all the items.

Java:
public void selectAll ( ) {
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:
public CheckBoxifiedTextView ( Context context, CheckBoxifiedText aCheckBoxifiedText ) {
super (context );

/* First CheckBox and the Text to the right (horizontal),
* not above and below (vertical) */

this. setOrientation (HORIZONTAL );
mCheckBoxText = aCheckBoxifiedText;
mCheckBox = new CheckBox (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, new LinearLayout. LayoutParams (
LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT ) );

mText = new TextView (context );
mText. setText (aCheckBoxifiedText. getText ( ) );
//mText.setPadding(0, 0, 15, 0);
addView (mText, new LinearLayout. 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 extends ListActivity {
/** Called when the activity is first created. */

private CheckBoxifiedTextListAdapter 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 void onCreate (Bundle icicle ) {
super. onCreate (icicle );
setContentView (R. layout. main );

cbla = new CheckBoxifiedTextListAdapter ( this );
for ( int k= 0; k length; k++ )
{
cbla. addItem ( new CheckBoxifiedText (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.

你可能感兴趣的:(java,UP)