如何选择/取消选中所有checkBox?

时间:2011-07-20 06:57:11

标签: android checkbox adapter

我有一个带有imageview的适配器,一个textView和一个Checkbox,还有一个“Select all”按钮,用于选中所有复选框。我搜索了很多关于如何做到这一点(选择所有复选框)但它没有用。任何人都可以解释我应该怎么做?请......我必须这么做

这是我的适配器:

Public class LazyAdapter1 extends BaseAdapter {

        private Activity activity;
        private String[] data;
        private String[] nume;
        private LayoutInflater inflater=null;
        public ImageLoader imageLoader; 

        public LazyAdapter1(Activity a, String[] d, String[] f) {
            activity = a;
            data=d;
            nume=f;
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            imageLoader=new ImageLoader(activity.getApplicationContext());
        }

        public int getCount() {
            return data.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public class ViewHolder{
            public TextView text;
            public ImageView image;
            public CheckBox ck;
        }



        public View getView(int position, View convertView, ViewGroup parent) {
            View vi=convertView;
            ViewHolder holder;
            if(convertView==null){
                vi = inflater.inflate(R.layout.item, null);
                holder=new ViewHolder();
                holder.text=(TextView)vi.findViewById(R.id.text);;
                holder.image=(ImageView)vi.findViewById(R.id.image);
                holder.ck=(CheckBox)vi.findViewById(R.id.chkbox);
                vi.setTag(holder);
            }
            else
                holder=(ViewHolder)vi.getTag();

            holder.text.setText(nume[position]);
            holder.image.setTag(data[position]);
            imageLoader.DisplayImage(data[position], activity, holder.image);
            return vi;
        }
    }

提前致谢。

3 个答案:

答案 0 :(得分:6)

boolean flag = true;

现在点击按钮 交换标志的值

flag = !flag;
adapter.notifydatasetchanged();

现在在你的getView方法中

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    ViewHolder holder;
    if (convertView == null) {
        vi = inflater.inflate(R.layout.item, null);
        holder = new ViewHolder();
        holder.text = (TextView) vi.findViewById(R.id.text);
        holder.image = (ImageView) vi.findViewById(R.id.image);
        holder.ck = (CheckBox) vi.findViewById(R.id.chkbox);
        vi.setTag(holder);
    } else {
        holder = (ViewHolder) vi.getTag();
    }
    holder.ck.setChecked(flag);
    holder.text.setText(nume[position]);
    holder.image.setTag(data[position]);
    imageLoader.DisplayImage(data[position], activity, holder.image);
    return vi;
}

答案 1 :(得分:1)

这将获取列表视图中的所有视图,并在每个视图中显示复选框并设置它们进行检查:

List<View> listOfViews = new ArrayList<View>();
listView1.reclaimViews(listOfViews);
for (View v : listOfViews)
{
    CheckBox cb = (CheckBox)v.findViewById(R.id.checkbox);
    cb.setChecked(true);
}

<强>更新

在初始化适配器之前,创建一个包含所有复选框的检查状态的arraylist。然后将此arraylist作为参数传递给自定义适配器并在getView()函数中使用(checkStates是以下代码中的arraylist):

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder holder;
        if(convertView==null){
            vi = inflater.inflate(R.layout.item, null);
            holder=new ViewHolder();
            holder.text=(TextView)vi.findViewById(R.id.text);;
            holder.image=(ImageView)vi.findViewById(R.id.image);
            CheckBox checkbox = (CheckBox)vi.findViewById(R.id.chkbox);
            checkbox.setChecked(checkStates.get(position);
            holder.ck= checkbox;
            vi.setTag(holder);
        }
        else
            holder=(ViewHolder)vi.getTag();

        holder.text.setText(nume[position]);
        holder.image.setTag(data[position]);
        holder.ck.setChecked(checkStates.get(position));
        imageLoader.DisplayImage(data[position], activity, holder.image);
        return vi;
    }

然后,当您想要检查所有复选框时,将包含复选框的检查状态的arraylist中的所有值设置为true。然后拨打listView1.setAdapter(adapter);

答案 2 :(得分:0)

我为每个选定项目分配了一个ID,然后使用HashMap跟踪所选项目