mercredi 19 juin 2019

How to select all rows of listview containing checkboxes with select all checkbox

I am trying to select all checkboxes of my listview with another SELECT ALL checkbox. I did some searching yet none of it works. The current SELECT ALL chcekbox only checks the last row of the listview, I need it to check all of the rows inside the listview.

Here's my getview method

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        View mView = convertView;
        String betid = mData.get(i).get("id");
        ViewHolder holder ;
        if (mView == null) {
            Context context = viewGroup.getContext();
            LayoutInflater inflater = LayoutInflater.from(context);
            mView = inflater.inflate(R.layout.row_layout, null,false);
            holder = new ViewHolder();
            holder.tx_number = (TextView) mView.findViewById(R.id.tx_number);
            holder.tx_amount = (TextView) mView.findViewById(R.id.tx_amount);
            holder.checkBox = (CheckBox) mView.findViewById(R.id.checkmark);
            selectAll = findViewById(R.id.selectAll);
            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @RequiresApi(api = Build.VERSION_CODES.N)
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (buttonView.isChecked()) {
                        checked.add((Integer) holder.checkBox.getTag());
                        holder.tx_amount.setBackgroundColor(getResources().getColor(R.color.bluelim));
                        holder.tx_number.setBackgroundColor(getResources().getColor(R.color.bluelim));
                    }
                    else {
                        holder.tx_amount.setBackgroundColor(Color.WHITE);
                        holder.tx_number.setBackgroundColor(Color.WHITE);
                        checked.remove((Integer) holder.checkBox.getTag());
                    }
                }
            });
            holder.checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            selectAll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for ( int i=0; i < listView.getChildCount(); i++) {
                        holder.checkBox.setChecked(true);
                    }
                }
            });
                mView.setTag(holder);
                holder.checkBox.setTag(i);
        } else {
            holder = (ViewHolder) mView.getTag();
            ((ViewHolder)mView.getTag()).checkBox.setTag(i);
        }
        if (betid != null) {
            String betnumber = mData.get(i).get("betnumber");
            String amountTarget = mData.get(i).get("betamount");
            holder.tx_amount.setText(amountTarget);
            holder.tx_number.setText(betnumber);
        }
        ViewHolder holde2r = (ViewHolder) mView.getTag();
        for (int k = 0; k < checked.size(); k++) {
            if (checked.get(k) == i) {
                holde2r.checkBox.setChecked(true);
            }
            else if (checked.get(k) != i) {
                holde2r.checkBox.setChecked(false);
            }
        }
        return mView;
    }




Aucun commentaire:

Enregistrer un commentaire