Android在listview添加checkbox实现

主界面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>


版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://my.oschina.net/u/1413001/blog/529274

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值