Checkbox Text List :: Extension of Iconified Text tutorial

创建带复选框的列表视图

 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. In CheckBoxifiedText.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 at CheckboxifiedTextViewAdapter.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 up CheckBoxifiedTextView.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 ( 00200 )// 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.java file. 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 (using cbla.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.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值