Checkbox Text List :: Extension of Iconified Text tutorial
2009-11-19 15:48
441 查看
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.
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.
We also want to add some methods for doing things like getting the state of the checkbox, or selecting all the items.
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.
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.
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.
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; } |
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; } |
Java: |
public void selectAll(){ for(CheckBoxifiedText cboxtxt: mItems) cboxtxt.setChecked(true); /* Things have changed, do a redraw. */ this.notifyDataSetInvalidated(); } |
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)); } |
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; klength; k++) { cbla.addItem(new CheckBoxifiedText(items[k], false)); } // Display it setListAdapter(cbla); } } |
相关文章推荐
- Iconified TextList - The making of
- 获取CheckBoxList所有选中项的值values或文本text
- How to make text of an html checkbox clickable (like ASP.NET control)
- 借助HiddenText 确定CheckBoxList当前的操作类型及点击的CheckBox
- vc ListControl设置checkbox风格,时owerdata不能设置为true---m_list.SetItemText()m_list.SetItemData()有关
- 借助HiddenText 确定CheckBoxList当前的操作类型及点击的CheckBox
- 为什么CheckBoxList 绑定数据后 要在读取不到Text 和Value ?
- ASPxCheckBoxList控件获取selected项的text和value的方法
- Jquery 获取Asp.Net CheckBoxList的Value,Text
- 近观Python: Texts as list of words
- Display selected items of a checkboxlist
- ListFragment+CheckedTextView和ListFragment+CheckBox两种多选效果
- 借助HiddenText 确定CheckBoxList当前的操作类型及点击的CheckBox
- List of resources: Article text extraction from HTML documents
- Is it possible to use a checkbox in a split button list instead of the gear icon?
- uva10420 - List of Conquests
- CheckBoxList是后台代码手动绑定的,前台页面怎么做全选啊?
- List of Scientist`s Names
- LeetCode之19---Remove Nth Node From End of List
- LeetCode Remove Nth Node From End of List