【问题标题】:all Check box selected by default in a group of an expandable list一组可展开列表中默认选中的所有复选框
【发布时间】:2016-05-18 17:15:13
【问题描述】:

我有一个包含checkbox 的可扩展列表,其中包括
MyCategory 和其他othercategory

我希望默认检查仅 mycategory 组中的所有 checkbox。 有人可以帮助我吗?

下面是我的代码

getChildView 我的BaseExpandableListAdapter 类的方法

    @Override
public View getChildView(final int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    LayoutInflater inflater = activity.getLayoutInflater();
    ViewHolder holder=null;

    final int mGroupPosition = groupPosition;
    final int mChildPosition = childPosition;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.category_list_row, null);
        holder= new ViewHolder();
        holder.mCheckBox=(CheckBox)convertView.findViewById(R.id.category_Login);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    /*
     * You have to set the onCheckChangedListener to null
     * before restoring check states because each call to
     * "setChecked" is accompanied by a call to the
     * onCheckChangedListener
    */
    holder.mCheckBox.setOnCheckedChangeListener(null);

    if (mChildCheckStates.containsKey(mGroupPosition)) {
        /*
         * if the hashmap mChildCheckStates<Integer, Boolean[]> contains
         * the value of the parent view (group) of this child (aka, the key),
         * then retrive the boolean array getChecked[]
        */
        boolean getChecked[] = mChildCheckStates.get(mGroupPosition);

        // set the check state of this position's checkbox based on the
        // boolean value of getChecked[position]
        holder.mCheckBox.setChecked(getChecked[mChildPosition]);

    } else {

        /*
         * if the hashmap mChildCheckStates<Integer, Boolean[]> does not
         * contain the value of the parent view (group) of this child (aka, the key),
         * (aka, the key), then initialize getChecked[] as a new boolean array
         *  and set it's size to the total number of children associated with
         *  the parent group
        */
        boolean getChecked[] = new boolean[getChildrenCount(mGroupPosition)];

        // add getChecked[] to the mChildCheckStates hashmap using mGroupPosition as the key
        mChildCheckStates.put(mGroupPosition, getChecked);

        // set the check state of this position's checkbox based on the
        // boolean value of getChecked[position]
        holder.mCheckBox.setChecked(false);
    }

    holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (isChecked) {

                boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
                getChecked[mChildPosition] = isChecked;
                mChildCheckStates.put(mGroupPosition, getChecked);
            } else {

                boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
                getChecked[mChildPosition] = isChecked;
                mChildCheckStates.put(mGroupPosition, getChecked);
            }
        }
    });

    return convertView;
}

【问题讨论】:

标签: android checkbox expandablelistview


【解决方案1】:

对于每个复选框做

checkbox.setChecked(true)

【讨论】:

  • 你最好解释一下为什么你的答案解决了他的问题,谢谢:)
猜你喜欢
  • 2016-09-03
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 2021-05-16
  • 1970-01-01
相关资源
最近更新 更多