可扩展列表中的组视图中的复选框

时间:2012-06-21 11:09:32

标签: android checkbox

我的情景:

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

我已经完成了这两个解决方案:

Android ExpandableListView CheckBox Select Only Clicked Checkbox

Check all checkbox in ExpandableListView (Android)

我在groupPosition 2中选中一个复选框,然后自动检查groupPosition 5中的复选框。再次当我在groupPosition 1中选中一个复选框时,会自动检查groupPosition 6中的复选框。我已经在Hash Map中保存了checkbox的状态。任何人都可以解释为什么会这样。

我的CodeView for childrenView如下

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;
}

我已经保存了哈希映射的状态,如下所示

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

1 个答案:

答案 0 :(得分:1)

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