notifyDataSetChanged for multiple checkboxes

后端 未结 7 1597
一向
一向 2020-12-10 14:09

\"multiple_checkboxes\"

In attached image, SelectAll checkbox is present with in an activity, and

7条回答
  •  轮回少年
    2020-12-10 14:39

    Here is a simple solution for you.

    @Override
    public View getView(int position, View v, ViewGroup arg2) {
        final PillTime pillTime = arrPillTimes.get(position);
        if(v == null) {
            v = inflater.inflate(R.layout.select_time_row, null);
        }
        TextView tvTitle = (TextView)v.findViewById(R.id.tvTitle);
        TextView tvTime = (TextView)v.findViewById(R.id.tvTime);
    
        tvTime.setText(pillTime.getTime());
    
        final CheckBox cbtime = (CheckBox)v.findViewById(R.id.cbtime);
        cbtime.setChecked(false);
        for (PillTime pillTime2 : arrSelectedTimes) {
            if(pillTime2.getId().equals(pillTime.getId())) {
                cbtime.setChecked(true);
                break;
            }
        }
        tvTitle.setText(pillTime.getTitle());
        cbtime.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                if(cbtime.isChecked()) {
                    boolean contains = false;
                    for (PillTime pillTime2 : arrSelectedTimes) {
                        if(pillTime2.getId().equals(pillTime.getId())) {
                            contains = true;
                            break;
                        }
                    }
                    if(!contains) {
                        arrSelectedTimes.add(pillTime);
                        if(arrPillTimes.size() == arrSelectedTimes.size()) {
                            cbSelectAll.setChecked(true);
                        }
                    }
                }else {
                    for (PillTime pillTime2 : arrSelectedTimes) {
                        if(pillTime2.getId().equals(pillTime.getId())) {
                            arrSelectedTimes.remove(pillTime2);
                            cbSelectAll.setChecked(false);
                            break;
                        }
                    }
                }
            }
        });
    
        return v;
    }
    

    I have one main arrayList arrPillTimes which has all the data and I have second arrayList arrSelectedTimes which will keep the track of checked items.We will check if selected item is in arrSelectedTimes it means you have checked that item so you have to set checked property to true for that checkbox.

提交回复
热议问题