【问题标题】:method in custom adapter to check all checkboxes自定义适配器中的方法来检查所有复选框
【发布时间】:2012-12-03 04:40:12
【问题描述】:

我正在尝试通过一键单击通过自定义适配器检查所有复选框。但是当我实现

ArrayList<BazarItems> objects

    public void allChecked(){

        for (int i=0; i<objects.size(); i++){

            cbBuy.setChecked(true);
        }

    }

最后一项被检查。 我做错了什么?

试过这个

`public void checkAll () {

        for (int i=0; i<objects.size(); i++){

            BazarItems b = objects.get(i);

            b.box = true;

            cbBuy.setChecked(b.box);
        }
    }   
    `

我们开始吧。我可能在自定义适配器上遗漏了一些东西。据我了解,我所拥有的应该足以完成任务。

`public class BazarListAdapter extends BaseAdapter {

    Context ctx;
    LayoutInflater lInflater;
    ArrayList<BazarItems> objects;

    CheckBox cbBuy;

    BazarListAdapter(Context context, ArrayList<BazarItems> products) {
        ctx = context;
        objects = products;
        lInflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.activity_bazar_list_child_item,
                    parent, false);
        }

        BazarItems i = getItems(position);

        ((TextView) view.findViewById(R.id.tvDescription)).setText(i.name);
        ((TextView) view.findViewById(R.id.tvQuantity))
                .setText(i.quantity + "");
        ((TextView) view.findViewById(R.id.tvPrice)).setText(i.price + "");

        cbBuy = (CheckBox) view.findViewById(R.id.checkBox);

        cbBuy.setOnCheckedChangeListener(myCheckChangList);

        cbBuy.setTag(position);

        cbBuy.setChecked(i.box);
        return view;
    }

    BazarItems getItems(int position) {
        return ((BazarItems) getItem(position));
    }

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

    OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            getItems((Integer) buttonView.getTag()).box = isChecked;
        }
    };

`

【问题讨论】:

    标签: android checkbox adapter


    【解决方案1】:

    make 归档在你的CustomAdapter

    boolean isAllCHecked=false;
    

    以及类似的设置方法

     public setAllChecked()
     {
        isAllCHecked=true;
        notifyDataSetChanged();
     } 
    

    并在您的CustomAdapter 中的getView 中执行类似的操作

    //get instance of the checkbox
    //call checkBox.setChecked(isAllCHecked);
    

    【讨论】:

    • 非常感谢莫辛。有用。在这个阶段的android知识中我自己找不到解决方案。
    【解决方案2】:

    你总是在同一个对象上调用 setChecked()。

    尝试:

        for (int i=0; i<objects.size(); i++){
    
            objects.get(i).box = true;
        }
    

    然后invalidate();你的 ListView。

    【讨论】:

    • 你的建议永远不会奏效,因为 BazarItems 中没有 setChecked (boolean) 方法
    • 我已经尝试了另一个(见编辑),但结果是一样的。
    • 对不起,我误会了。显示您的 CustomAdapter。
    猜你喜欢
    • 2014-03-20
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 2014-05-20
    • 2019-05-27
    • 1970-01-01
    相关资源
    最近更新 更多