如何使用带有expandedlistview的选择所有复选框?

时间:2019-03-15 05:12:06

标签: java android android-checkbox selectall

这是我设置子视图的代码。我将第一个Child设置为select all布局,然后选择其余的子布局。当我按下全选复选框时,什么也没有发生。

public View getChildView(int groupPosition, final int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            ArrayList<CheckBox> myCheckBoxes = new ArrayList<>();

            if(childPosition == 0){
                convertView = inflater.inflate(R.layout.child_header, null);
                chkBoxselectAll = convertView.findViewById(R.id.chk_select_all);

            } else {
                convertView = inflater.inflate(R.layout.child_row, null);
                chkBoxsurvey = convertView.findViewById(R.id.chk_survey);
                for (int i = 0; i < parent.getChildCount(); i++) {
                    View v = parent.getChildAt(i);
                        if(v.getId() == (R.id.chk_survey))
                            myCheckBoxes.add((CheckBox)v.findViewById(R.id.chk_survey));
                }
            }
 if (childPosition > 0 && childPosition < getChildrenCount(groupPosition) - 1) {

            chkBoxselectAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

                    for (int i = 0; i < myCheckBoxes.size(); i++) {
                        if(isChecked)
                            myCheckBoxes.get(i).setChecked(true);
                        else
                            myCheckBoxes.get(i).setChecked(false);
                    }
    }
            return convertView;
        }

1 个答案:

答案 0 :(得分:0)

我没有看到任何通知您的列表的声明,因此您需要为此添加一行。

参见下面的代码,我添加了 notifyDataSetChanged 外循环

  for (int i = 0; i < myCheckBoxes.size(); i++) {
     if(isChecked)
        myCheckBoxes.get(i).setChecked(true);
     else
        myCheckBoxes.get(i).setChecked(false);
   }
   notifyDataSetChanged();