如何选择/取消选择所有复选框?

发布于 2024-11-25 08:03:46 字数 1927 浏览 4 评论 0原文

我有一个带有图像视图、文本视图和复选框的适配器,以及用于选择所有复选框的“全选”按钮。我搜索了很多关于如何执行此操作(选择所有复选框)的信息,但它不起作用。谁能解释更多我应该做什么?拜托...我必须做这件事紧急

这是我的适配器:

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;
        }
    }

提前致谢。

I have an adapter with an imageview,a textView and a Checkbox and a button "Select all" for selecting all the checkbox. I searched a lot about how can I do this (select all checkbox) but it not worked. Can anyone explain more what should I do? Please...I have to do this urgent

Here is my adapter :

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;
        }
    }

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谜兔 2024-12-02 08:03:46
boolean flag = true;

现在点击你的按钮
交换 flag Now 的值

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;
}
boolean flag = true;

Now on your button click
swap the value of flag

flag = !flag;
adapter.notifydatasetchanged();

Now in your getView method

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;
}
鱼窥荷 2024-12-02 08:03:46

这将获取列表视图中的所有视图,并将获取每个视图中的复选框并将它们设置为选中:

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

更新:

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

    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;
    }

然后,当您想要检查所有复选框时,设置包含 checkstates 的数组列表中的所有值复选框为 true。然后调用listView1.setAdapter(adapter);

This will get all the views in your listview and will get the checkbox in each view and sets them to be checked:

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

UPDATE:

Before initializing the adapter, create an arraylist that contains the checkstates of all the checkboxes. Then pass this arraylist as an argument to your custom adapter and use in the getView() function (checkStates is the arraylist in the below code):

    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;
    }

Then when you want to check all the checkboxes, set all the values in the arraylist containing the checkstates of the checkboxes as true. Then call listView1.setAdapter(adapter);

梨涡 2024-12-02 08:03:46

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

I asigned an id to each selected item and then use a HashMap to keep track of the selected items

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文