ExpandableListView检查多个项目

时间:2014-05-30 15:53:05

标签: android expandablelistview android-checkbox

我正在使用从数据库填充的ExpandableListView。数据库中的列有助于确定哪些项目已选中CheckBox。单击CheckBox更新项目的数据库行,初始化该变量。但是,我在滚动时遇到问题,列表保留了正确的顺序和CheckBox选择。

这是我最初的尝试。它在某种程度上起作用,因为我可以正确填充列表,但单击时复选框不会保持检查状态。

   @Override
public View getGroupView(final int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {


    if (convertView == null)
    {
        convertView = mInflater.inflate(R.layout.group_view, null);
    }
        //convertView = mInflater.inflate(R.layout.group_view, parent, false);

        TextView tv = (TextView) convertView.findViewById(R.id.textTitle);
        CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.favorite);
        tv.setText(getGroup(groupPosition).toString());
        checkBox.setFocusable(false);

        final int favorite = Integer.parseInt(getGroupFavorite(groupPosition).toString());
        if (favorite == 1)
        {
            checkBox.setChecked(true);
        }

        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (!((CheckBox) view).isChecked())
                {
                    // Log.v("checkBox", "checked, setting to unChecked");

                    myDB.updateFavorite(favoriteHelperList[groupPosition], "0");
                }

                // has been set from checked to unchecked
                else
                {
                    // Log.v("checkBox", "unChecked, setting to Checked");

                    myDB.updateFavorite(favoriteHelperList[groupPosition], "1");
                    // Log.v("database", "database updated");
                }

                notifyDataSetChanged();

            }
        });

    return convertView;
}

在挖掘之后,我发现了几个提到ViewHolder,并且一直在尝试,但无济于事。

    @Override
public View getGroupView(final int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {

    ViewHolderGroup viewHolderGroup;

    if (convertView == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(R.layout.group_view, parent, false);

        viewHolderGroup = new ViewHolderGroup();
        viewHolderGroup.textView = (TextView) convertView.findViewById(R.id.textTitle);
        viewHolderGroup.checkBox = (CheckBox) convertView.findViewById(R.id.favorite);

        convertView.setTag(viewHolderGroup);
    }

    else {
        viewHolderGroup = (ViewHolderGroup) convertView.getTag();
    }

    viewHolderGroup.favorite = Integer.parseInt(getGroupFavorite(groupPosition).toString());
    viewHolderGroup.textView.setText(getGroup(groupPosition).toString());
    viewHolderGroup.checkBox.setFocusable(false);

    if(viewHolderGroup.favorite == 1) {
        viewHolderGroup.checkBox.setChecked(true);
    }

    if (viewHolderGroup.textView != null) {
        viewHolderGroup.textView.setText(getGroup(groupPosition).toString());
        viewHolderGroup.textView.setTag(getGroupId(groupPosition));
    }
return convertView;
}

我做错了什么,我该如何解决?让列表填充一次,并使按钮正确更新。感谢

1 个答案:

答案 0 :(得分:0)

在我遇到同样的问题之前的几天,我通过使用这段由我编写的代码解决了这个问题......

explistView.ChildClick += (object pobjSender, ExpandableListView.ChildClickEventArgs    childArgs) => 
        {

            if(childstyle [listGroup [childArgs.GroupPosition]]   [childArgs.ChildPosition].checkedStatus){
                childstyle [listGroup[childArgs.GroupPosition]][childArgs.ChildPosition].checkedStatus=false;
                selectedServices.Remove(childstyle [listGroup[childArgs.GroupPosition]][childArgs.ChildPosition]);

            } else{
                childstyle[listGroup[childArgs.GroupPosition]][childArgs.ChildPosition].checkedStatus=true;
                //if(selectedServices.Contains(childstyle[listGroup[childArgs.GroupPosition]][childArgs.ChildPosition])){
                if (!selectedServices.Exists(x => x.ServiceID==childstyle[listGroup[childArgs.GroupPosition]][childArgs.ChildPosition].ServiceID)){
                    selectedServices.Add(childstyle[listGroup[childArgs.GroupPosition]][childArgs.ChildPosition]);
                }

            }
            expListAdapter.NotifyDataSetChanged();
        };


    }

childstyle是我的字典对象。 希望你能理解它。这很复杂。