1

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

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

`

4

2 回答 2

2

归档在你的CustomAdapter

boolean isAllCHecked=false;

和这样的设置方法

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

getViewCustomAdapter做这样的事情

//get instance of the checkbox
//call checkBox.setChecked(isAllCHecked);
于 2012-12-02T20:21:16.440 回答
0

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

尝试:

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

        objects.get(i).box = true;
    }

然后invalidate();是你的 ListView。

于 2012-12-02T19:44:06.497 回答