checkBox在可扩展列表中的第9个或第10个元素之后重复其状态

时间:2015-09-25 05:26:32

标签: android listview android-5.0-lollipop expandablelistview

如何在expandable listview中保存复选框的状态。

我使用Expandable ListView以父子形式显示数据,父列表视图工作正常但在子视图中它在滚动时随机更改其复选框状态。

这是我的代码,这些是BaseExpandableListAdapter的方法

这是滚动并随机选中多个复选框时创建问题的childView方法。

private String[] groups = {
                             "Gluten",
                             "Wheat",
                             "Peanuts"
                          };

private String[][] children = {
                              {"item1", "item2","item3","item4","item5","item6","item7"},
                              {"item1", "item2","item3","item4","item5","item6","item7"},
                              {"item1", "item2","item3","item4","item5","item6","item7"}
};
@Override
public int getChildTypeCount() {
    return children.length;
}

@Override
public int getGroupTypeCount() {
    return groups.length;
}

@Override 
public View getChildView(int i,final int i1,final boolean b, View view,
                                                ViewGroup viewGroup) {
   if(view == null)
        {
            view = LayoutInflater.from(ProfileActivity.this).inflate(R.layout.list_item, null);
            childHolder = new ChildHolder();
            childHolder.cCheckBox = (CheckBox) view.findViewById(R.id.checkbox2);
            childHolder.cTextView = (TextView) view.findViewById(R.id.lblListItem);

            view.setTag(childHolder);
            Log.e(TAG, "  if" + b+"");
        }
        else
        {
            Log.e(TAG, "else" + b+"");
            childHolder = (ChildHolder) view.getTag();
        }
        Log.e("TAG", "getchildView:" + b + " :: state :" + childHolder.state + " :: childHolder :" + childHolder.toString());

        childHolder.cTextView.setText(getChild(i, i1).toString());
        childHolder.cCheckBox.setChecked(childHolder.state);
        childHolder.cCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.e("child Check Box", childHolder.cTextView.getText() + "==========" + isChecked);
                childHolder.state = isChecked;
            }
        });
        return view;
} 

这些是视图持有者内部类

class GroupHolder { 
    CheckBox checkBox;
    TextView title;
    ChildHolder childHolder;
} 

class ChildHolder{ 
    CheckBox cCheckBox;
    TextView cTextView;
    Boolean state = false;
}

在图像中我只检查了一个子项,但随机选择了其他子项[{3}}

4 个答案:

答案 0 :(得分:0)

最好在列表视图中使用ImageView drawable而不是checkboxex。 但是你仍然可以保持复选框的状态。尝试在checkBox上设置setTag()和getTag(),并使用布尔数组来存储已检查和未检查的状态。

答案 1 :(得分:0)

将此代码添加到适配器。

@Override
    public int getViewTypeCount() {
        return Arraylist.size();
    }

答案 2 :(得分:0)

在Android ListView中,项目的单个视图将被回收,而不是为所有项目创建新对象。因此,在这种情况下,可能会有一个随机视图,可能会在之前的视图中重复。为了解决这个问题,应该每次使用相应的持有者数据更新项目视图:

尝试修改代码,然后在复选框中设置OnCheckedChangeListener,如下所示:

public View getChildView(int i,final int i1,final boolean b, View view,
                                                ViewGroup viewGroup) {

      if(view == null){
       ....
      }else{
       ....
      }
      ....
      childHolder.cTextView.setText(getChild(i, i1).toString());
      childHolder.cCheckBox.setChecked(childHolder.state);  //add this line here
      childHolder.cCheckBox.setOnCheckedChangeListener(...){
      .....
      }
}

答案 3 :(得分:0)

最后我得到了拯救国家的答案。

    @Override
    public View getChildView(final int i,final int i1,final boolean b, View view, ViewGroup viewGroup) {

        if(view == null)
        {
            view = LayoutInflater.from(ProfileActivity.this).inflate(R.layout.list_item, null);
            childHolder = new ChildHolder();
            childHolder.cCheckBox = (CheckBox) view.findViewById(R.id.checkbox2);
            childHolder.cTextView = (TextView) view.findViewById(R.id.lblListItem);

            view.setTag(childHolder);
        }
        else
        {
            childHolder = (ChildHolder) view.getTag();
        }

        childHolder.cTextView.setText(getChild(i, i1).toString());
        childHolder.cCheckBox.setOnCheckedChangeListener(null);    //to disable click listener
        childHolder.cCheckBox.setChecked(Boolean.parseBoolean(childrenState[i][i1]));  //setting the state here
        childHolder.cCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                childrenState[i][i1] = isChecked+"";
            }
        });
        return view;
    }

并且对于州我创建了一个类似的数组 要在不太好的实践中创建这样的数组,我们必须动态创建它,

private String childState[][]={{"false", "false","false","false","false","false","false"}, 
                          {"false", "false","false","false","false","false","false"}, 
                          {"false", "false","false","false","false","false","false"} 

};