在ExpandableListView中折叠组中丢失所选项目

时间:2014-05-27 08:28:27

标签: android android-listview

我无法保留关于group崩溃的检查数据.Below是我的适配器代码,请建议我一些想法。

public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private ExpandableListView mExpandableListView;
    private List<GroupModel> arrGroups;
    private int[] groupStatus;
    Boolean isActive = false;

    public ExpandableListViewAdapter(Context pContext,
            ExpandableListView pExpandableListView,
            List<GroupModel> pGroupCollection) {
        mContext = pContext;
        arrGroups = pGroupCollection;
        mExpandableListView = pExpandableListView;
        groupStatus = new int[arrGroups.size()];

    }

    @Override
    public String getChild(int arg0, int arg1) {
        return arrGroups.get(arg0).getChildArr().get(arg1).getName();
    }

    @Override
    public long getChildId(int arg0, int arg1) {
        return arg1;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,boolean arg2, View convertView, ViewGroup parent) {
        final ChildHolder childHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.exp_child_row, null);
            childHolder = new ChildHolder();
            childHolder.checkBox = (CheckBox) convertView
                    .findViewById(R.id.exp_child_chk);
            childHolder.name = (TextView) convertView
                    .findViewById(R.id.exp_child_txt);          
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        childHolder.name.setText(arrGroups.get(groupPosition).getChildArr().get(childPosition).getName());
        childHolder.checkBox.setChecked(arrGroups.get(groupPosition).getChildArr().get(childPosition).isSelected());

        childHolder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                arrGroups.get(groupPosition).getChildArr().get(childPosition).setSelected(isChecked);
                childHolder.checkBox.setChecked(arrGroups.get(groupPosition).getChildArr().get(childPosition).isSelected());

                Log.v("onCheckedChanged ::: ", groupPosition+ ":"+childPosition+"State>>> "+arrGroups.get(groupPosition).getChildArr().get(childPosition).isSelected());

            }
        });
        return convertView;
    }

    @Override
    public int getChildrenCount(int arg0) {
        return arrGroups.get(arg0).getChildArr().size();
    }

    @Override
    public Object getGroup(int arg0) {
        return arrGroups.get(arg0);
    }

    @Override
    public int getGroupCount() {
        return arrGroups.size();
    }

    @Override
    public long getGroupId(int arg0) {
        return arg0;
    }

    @Override
    public View getGroupView(int groupPosition, boolean arg1, View view,ViewGroup parent) {
        GroupHolder groupHolder;

        if (view == null) {
            view = LayoutInflater.from(mContext)
                    .inflate(R.layout.exp_grp, null);
            groupHolder = new GroupHolder();
            groupHolder.checkBox = (CheckBox) view.findViewById(R.id.exp_grp_chk);
            groupHolder.title = (TextView) view.findViewById(R.id.exp_grp_txt);
            view.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) view.getTag();
        }
        groupHolder.title.setText(arrGroups.get(groupPosition).getName());
        return view;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }

    class GroupHolder {
        CheckBox checkBox;
        TextView title;
    }

    class ChildHolder {
        CheckBox checkBox;
        TextView name;
    }


}

1 个答案:

答案 0 :(得分:3)

if else 条件之后的getChildView()方法中添加这些行。

if(arrGroups.get(groupPosition).getChildArr().get(childPosition).getSelected() ==    true){
 childHolder.checkBox.setChecked(true);
}else{
childHolder.checkBox.setChecked(false);
}

从子视图中删除 checkBox点击侦听器

在活动中为OnChildClickListener添加此ExpandableListView

expListView.setOnChildClickListener(new OnChildClickListener() {

        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            yourArray.get(groupPosition).getChildArr().get(childPosition).setSelected(true);
            yourAdapter.notifyDataSetChanged();
            return true;
        }
    });

});