【问题标题】:Checkbox in GroupView in ExpandableListExpandableList 中 GroupView 中的复选框
【发布时间】:2012-06-21 11:09:31
【问题描述】:

我的场景:

我在可扩展列表的 groupView 和 childView 中有一个列表。 GroupView 和 ChildView 有复选框。我设置复选框没有问题。当 GroupView 中的复选框被选中时,我想检查 ChilView 的所有复选框。

我经历过这两种解决方案:

Android ExpandableListView CheckBox Select Only Clicked Checkbox

Check all checkbox in ExpandableListView (Android)

我选中 groupPosition 2 中的复选框,然后自动选中 groupPosition 5 中的复选框。再次,当我选中 groupPosition 1 中的复选框时,say groupPosition 6 中的复选框会自动被选中。我已经在哈希图中保存了复选框的状态。谁能解释为什么会这样。

我的儿童视图代码如下

public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    final ChildHolder holder;
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.group_item, null);
        holder = new ChildHolder();
        holder.cb = (CheckBox) convertView.findViewById(R.id.cb);
        holder.title = (TextView) convertView.findViewById(R.id.title);
        convertView.setTag(holder);
    } else {
        holder = (ChildHolder) convertView.getTag();
    }

    Item item = getChild(groupPosition, childPosition);
    holder.title.setText(item.name);
    if (DataHolder.selectedGroupMembers.containsKey(groupPosition) == true)
        holder.cb.setChecked(true);
    else {
        holder.cb.setChecked(false);
    }
    return convertView;
}

我的 groupView 代码如下

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

    final GroupHolder holder;

    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.group_list, null);
        holder = new GroupHolder();
        holder.cb = (CheckBox) convertView.findViewById(R.id.cb);
        holder.imageView = (ImageView) convertView
                .findViewById(R.id.label_indicator);
        holder.title = (TextView) convertView.findViewById(R.id.title);
        convertView.setTag(holder);
    } else {
        holder = (GroupHolder) convertView.getTag();
    }

    holder.imageView
            .setImageResource(groupStatus[groupPosition] == 0 ? R.drawable.group_down
                    : R.drawable.group_up);
    Item groupItem = getGroup(groupPosition);

    holder.title.setText(groupItem.name);

    if (DataHolder.selectedGroupMembers.containsKey(groupPosition) == true) {
        holder.cb.setChecked(true);

    } else {
        holder.cb.setChecked(false);

    }


    holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {


        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            if (isChecked == true) {
                DataHolder.selectedGroupMembers.put(groupPosition,
                        new Boolean(true));
                Log.i("Prativa Clicked", "group" + groupPosition);
                notifyDataSetChanged();

            } else if (DataHolder.selectedGroupMembers
                    .containsKey(groupPosition)) {
                DataHolder.selectedGroupMembers.remove(groupPosition);
                Log.i("Prativa Removed ", "group" + groupPosition);
                notifyDataSetChanged();
            }
        }

    });

    return convertView;
}

我保存了hash map的状态如下

    public static HashMap<Integer,Boolean> selectedGroupMembers =     new HashMap<Integer, Boolean>();

【问题讨论】:

  • 发布您的代码很有帮助。
  • @Prativa 这里的 DataHolder 是什么。它是一个字符串或对象
  • 它是一个静态类,用于保存整个应用程序中使用的数据。
  • 嗨 Prativa.. 我有 1 个问题.. 如果选择了第 0 组,那么所有的孩子都被选中了。如果我取消选中第 1 个孩子(第 0 组内的 10 个孩子中)必须取消选中该组。请帮帮我。

标签: android checkbox


【解决方案1】:

因此我找到了解决方案。完整的解决方案作为最终编辑集成在问题本身中

【讨论】:

  • 嗨.. prativa...你解决了这个问题..?请简要解释一下,以便我可以实施,因为我有同样的问题..
  • @Sulabh Gajjar,我已经编辑了问题以包含解决方案。
猜你喜欢
  • 2015-07-07
  • 2014-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-27
  • 2014-10-07
  • 1970-01-01
相关资源
最近更新 更多