checkbox的全选、反选、删除(适配器)

package com.example.adapter;

import java.util.List;

import com.example.ay.R;
import com.example.vo.Flag;

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

public class LVadapter extends BaseAdapter {
    int[] str = new int[] { R.drawable.a1, R.drawable.a2, R.drawable.a22,
            R.drawable.a3, R.drawable.d3, R.drawable.h7, R.drawable.r,
            R.drawable.r4, R.drawable.s3, R.drawable.tt, R.drawable.vv,
            R.drawable.x1, R.drawable.y6 };

    List<Flag> list;

    public LVadapter(List<Flag> list, Context context) {
        super();
        this.list = list;
        this.context = context;
    }

    Context context;

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = convertView.inflate(context, R.layout.lv, null);
        ImageView image = (ImageView) convertView.findViewById(R.id.image);
        TextView name = (TextView) convertView.findViewById(R.id.name);
        CheckBox check = (CheckBox) convertView.findViewById(R.id.check);

        image.setImageResource(str[position]);
        name.setText(list.get(position).getName());

        check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {

                list.get(position).setFlag(isChecked);

            }
        });
        
        //记录当前复选框的操作
        check.setChecked(list.get(position).isFlag());
        return convertView;
    }

}

原文地址:https://www.cnblogs.com/1995yu/p/5389892.html