主界面checkboxinlistviewactivity.java代码如下:
复制代码 代码如下:
public class checkboxinlistviewactivity extends activity {
/** called when the activity is first created. */
private myadapter adapter;
private listview listview;
private button checkall;
private button nocheckall;
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
listview = (listview)findviewbyid(r.id.listview);
checkall = (button)findviewbyid(r.id.button1);
nocheckall = (button)findviewbyid(r.id.button2);
adapter = new myadapter();
listview.setadapter(adapter);
checkall.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
adapter.checkall();
}
});
nocheckall.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
adapter.nocheckall();
}
});
}
private class myadapter extends baseadapter{
private arraylist<message> list= new arraylist<message>();
public myadapter(){
for(int i = 1 ;i<=100 ; i++){
list.add(new message("item_"+i));
}
}
public void checkall(){
for(message msg:list){
msg.ischeck = true;
}
notifydatasetchanged();
}
public void nocheckall(){
for(message msg:list){
msg.ischeck = false;
}
notifydatasetchanged();
}
@override
public int getcount() {
return list.size();
}
@override
public object getitem(int position) {
return null;
}
@override
public long getitemid(int position) {
return 0;
}
@override
public view getview(int position, view convertview, viewgroup parent) {
viewholder viewholder;
if(convertview == null){
layoutinflater inflater = layoutinflater.from(checkboxinlistviewactivity.this);
convertview = inflater.inflate(r.layout.listview_item, null);
viewholder = new viewholder();
viewholder.checkbox = (checkbox)convertview.findviewbyid(r.id.checkbox1);
convertview.settag(viewholder);
}else{
viewholder = (viewholder)convertview.gettag();
}
final message msg = list.get(position);
viewholder.checkbox.settext(msg.str);
viewholder.checkbox.setchecked(msg.ischeck);
//注意这里设置的不是oncheckedchanglistener,还是值得思考一下的
viewholder.checkbox.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
if(msg.ischeck){
msg.ischeck = false;
}else{
msg.ischeck = true;
}
}
});
return convertview;
}
}
private class viewholder{
checkbox checkbox;
}
}
适配器所适配的消息message.java如下:
复制代码 代码如下:
public class message {
public boolean ischeck;
public string str;
public message(string str){
this.str = str;
}
}
main.xml代码如下:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<linearlayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<button android:text="全选" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></button>
<button android:text="撤消" android:id="@+id/button2"
android:layout_width="wrap_content" android:layout_height="wrap_content"></button>
</linearlayout>
<listview android:id="@+id/listview" android:layout_height="fill_parent"
android:layout_width="fill_parent" />
</linearlayout>
listview_item.xml代码如下:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantfocusability="blocksdescendants"
>
<linearlayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<checkbox
android:text="checkbox"
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</linearlayout>
</linearlayout>
囧神的世界你不懂,虫哥的生活你没有,只有程序猿的世界大家才知道。程序猿们,为了自己的精彩世界奋斗吧,努力吧!加油……