【问题标题】:Uncheck SelectAll Checkbox if unselect listview checkbox如果取消选择列表视图复选框,请取消选中 SelectAll 复选框
【发布时间】:2017-02-17 17:38:39
【问题描述】:

我有一个带有复选框的自定义列表视图。在主布局中有“全选”复选框。如果我选择“全选”复选框,那么所有复选框都会被选中。我想要的是当我取消选择任何一个列表视图复选框时,“全选”复选框也必须被取消选中。我尝试了here,但它不起作用。如果我取消选择任何列表视图复选框,则所有列表视图复选框都会被取消选中。

public class ContactsListAdapter extends BaseAdapter {
    Context ctx;
    LayoutInflater lInflater;
    ArrayList<Contacts> objects;
    CheckBox _selectall;

    ContactsListAdapter(Context context, ArrayList<Contacts> products, CheckBox selectall) {
        ctx = context;
        objects = products;
        lInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        _selectall=selectall;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public Object getItem(int position) {
        return objects.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.contact_item, parent, false);
        }
        //final Contacts p = getProduct(position);
        Contacts contacts = objects.get(position);

        ((TextView) view.findViewById(R.id.tvName)).setText(contacts.getName());
        ((TextView) view.findViewById(R.id.tvMobile)).setText(contacts.getMobile());
        ((TextView) view.findViewById(R.id.tvEmail)).setText(contacts.getEmail());

        final CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox);

        cbBuy.setTag(position);
        cbBuy.setChecked(contacts.box);
        if(_selectall.isChecked()){
            _selectall.setChecked(true);
        }else{
            _selectall.setChecked(false);
        }

        cbBuy.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                getProduct((Integer) buttonView.getTag()).box = isChecked;

                if(_selectall.isChecked()){
                    _selectall.setChecked(true);
                }else{
                    _selectall.setChecked(false);
                }
                notifyDataSetChanged();

            }
        });

        _selectall.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {


                if(_selectall.isChecked()){
                    for(int i=0; i<objects.size();i++){
                        getProduct(i).box = true;
                        cbBuy.setTag(i);
                        cbBuy.setChecked(objects.get(i).box);
                    }

                }else{
                    for(int i=0; i<objects.size();i++){
                        getProduct(i).box = false;
                        cbBuy.setTag(i);
                        cbBuy.setChecked(objects.get(i).box);
                    }
                }

                notifyDataSetChanged();
            }

        });
        return view;
    }
    Contacts getProduct(int position) {
        return ((Contacts) getItem(position));
    }



    ArrayList<Contacts> getBox() {
        ArrayList<Contacts> box = new ArrayList<Contacts>();
        for (Contacts p : objects) {
            if (p.box)
                box.add(p);
        }
        return box;
    }


}

【问题讨论】:

标签: android listview android-checkbox


【解决方案1】:

在 checkedChangedListener 上检查已检查项目的总数。如果它等于您的总项目,则 selectAll 复选框将为真,否则为假。详细示例为here

【讨论】:

    【解决方案2】:

    我相信您的全选复选框不在您的列表组件中。 您现在不需要维护地图。

    将您的逻辑移至 SelectAll setOnCheckedChangeListener 放置 来自适配器的 getView()。它只会调用 notifyDatasetChanged(),没有比这更多的一行。

    所以首先你只需要确保 setOnCheckedChangeListener for selectAll 调用 notifyDatasetChanged for listview,所以所有的列表复选框组件都会被修改。

    在 listview 的 getView 方法中,只需在设置复选框时添加检查 对于单个项目,即 if(selectAll.isChecked()) 然后标记这个 复选框为 setSelected(true) 否则为 false。

    特定列表项的 setOnCheckedChangeListener 上的最后一个 复选框添加与上面相同的检查,即 if(selectAll.isChecked()) 但只需将 SelectAllCheckbox 设置为 false。

    希望这能解决您的问题。

    【讨论】:

    • 所以我根据您的建议进行了编辑。当前的问题是,当我取消选择任何一个列表视图复选框(选择标题复选框后)时,列表视图复选框会被取消选中,但标题复选框不会被选中 @MobileEvangelist
    • 不工作。我在Activity中添加了标题复选框onchangelistener,只保留了adapter.notifyDatasetChanged。并在 listview 复选框 chnagelistener if(_selectall.isChecked()){ _selectall.setChecked(false); }else{ _selectall.setChecked(true); }
    • 你添加了 if(_selectall.isChecked()){ cBuy.setChecked(true); }else{ cBuy.setChecked(false); } 在 getView() 中的 findviewById 和 setTag 部分之后..?
    • 是的。如果你给我看一些代码让我可以匹配会更好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 2023-04-02
    • 1970-01-01
    • 2014-05-04
    相关资源
    最近更新 更多