android 中全选 反选 checkbox

原创
2016/04/13 12:32
阅读数 107
MainActivity

public class MainActivity extends Activity {

    private Button button1;
    private Button button2;
    private Button button3;
    private ListView listView;
    private List<Good> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = (Button) findViewById(R.id.button1);//全选
        button2 = (Button) findViewById(R.id.button2)//反选
        button3 = (Button) findViewById(R.id.button3);//全不选
        listView = (ListView) findViewById(R.id.listveiw);
        list = new ArrayList<Good>();
        for (int i = 0; i < 60; i++) {
            list.add(new Good("lallalalall"+i, false));
        }
        
      //  BaseAdapter adapter=new Adapter(list,MainActivity.this,button1,button2,button3);
       BaseAdapter  adapter=new Adapter(list, getApplicationContext(), button1, button2, button3);
        listView.setAdapter(adapter);
        
      
    }

}

适配器

package com.example.rikao_0413;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class Adapter extends BaseAdapter{
    List<Good> list;
    Context context;
    Button button1;
    Button button2;
    Button button3;
    public Adapter(List<Good> list, Context context, Button button1, Button button2, Button button3) {
        // TODO Auto-generated constructor stub
    this.list=list;
    this.context=context;
    this.button1=button1;
    this.button2=button2;
    this.button3=button3;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHolder viewHolder;
        if(convertView==null){
            convertView=View.inflate(context, R.layout.item, null);
         viewHolder=new ViewHolder();
         viewHolder.textView=(TextView) convertView.findViewById(R.id.text);
        viewHolder.checkBox=(CheckBox) convertView.findViewById(R.id.checkbox);
        convertView.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder) convertView.getTag();
        }
        viewHolder.textView.setText(list.get(position).getName());
        
        //checkbox监听
        viewHolder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                 if(isChecked){
                    
                     list.get(position).setBo(isChecked);
                    
                }else{
                    
                    list.get(position).setBo(isChecked);
                    
                }

            }
        });
        //全选
        button1.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                // TODO Auto-generated method stub
                for (int i = 0; i < list.size(); i++) {
                    //改变boolean
                    list.get(i).setBo(true);
                    //刷新
                    notifyDataSetChanged();
                }
                
            }
        });
        
        //反选
        button2.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                // TODO Auto-generated method stub
                for (int i = 0; i < list.size(); i++) {
                    if(list.get(i).getBo()){
                        list.get(i).setBo(false);
                    }else{
                        list.get(i).setBo(true);
                    }
                    notifyDataSetChanged();
                }
            }
        });
        //全不选
        button3.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                // TODO Auto-generated method stub
                for (int i = 0; i < list.size(); i++) {
                    
                    list.get(i).setBo(false);
                    
                    notifyDataSetChanged();
                }
            }
        });
        //显示checkBox
        viewHolder.checkBox.setChecked(list.get(position).getBo());
        return convertView;
    }

class ViewHolder{
    TextView textView;
    CheckBox checkBox;
}
}

Good 类的属性

private String name;
    private boolean bo;

展开阅读全文
打赏
1
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
1
分享
返回顶部
顶部