user1706819
user1706819

Reputation: 125

method in custom adapter to check all checkboxes

I am trying to check all checkboxes through the custom adapter with one button click. But when I implement

ArrayList<BazarItems> objects

    public void allChecked(){

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

            cbBuy.setChecked(true);
        }

    }

the only last item is checked. What am I doing wrong?

have tried this one

`public void checkAll () {

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

            BazarItems b = objects.get(i);

            b.box = true;

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

here we go. I am probably missing something on custom adapter. In my understanding what I have should be enough to complete task.

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

`

Upvotes: 1

Views: 1388

Answers (2)

mirekkomis
mirekkomis

Reputation: 503

You call setChecked() always on the same object.

try:

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

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

and then invalidate(); your ListView.

Upvotes: 0

Mohsin Naeem
Mohsin Naeem

Reputation: 12642

make filed in your CustomAdapter

boolean isAllCHecked=false;

and setter method for this like

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

and in getView of your CustomAdapter do some thing like this

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

Upvotes: 2

Related Questions