jeudi 11 juillet 2019

How to set the checkBox check by default on the first child of all the Groups In ExpandableListView

Implemented ExpandableListview, After expanding for the childViews I am setting a checkBox. how can I set first child selected by default on all the groups, And after reloading the listView how can I hold the checkBox status of childView checkBox.

Find the below code which I implemented in BaseExpandableListAdapter

 @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

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

        ChildViewHolder viewHolder = null;

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.filter_child, null);

            viewHolder = new ChildViewHolder();

            viewHolder.checkBox1 = (CheckBox)convertView.findViewById(R.id.check);
            viewHolder.checkBox1.setFocusable(false);

            viewHolder.mChildText = (TextView) convertView
                    .findViewById(R.id.lblListItemFilter);

            //viewHolder.checkBox1 = (CheckBox)convertView.findViewById(R.id.check);

            convertView.setTag(R.layout.filter_child, viewHolder);
            //viewHolder.checkBox2 = (CheckBox)convertView.findViewById(R.id.check);
        }else {
            viewHolder = (ChildViewHolder) convertView
                    .getTag(R.layout.filter_child);
        }

       // TextView txtListChild = (TextView) convertView
         //       .findViewById(R.id.lblListItemFilter);

        viewHolder.mChildText.setText(childText);

        viewHolder.checkBox1.setOnCheckedChangeListener(null);

        if (mChildCheckStates.containsKey(mGroupPosition)){

            boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
            viewHolder.checkBox1.setChecked(getChecked[mChildPosition]);
        }else {
            boolean getChecked[] = new boolean[getChildrenCount(mGroupPosition)];

            mChildCheckStates.put(mGroupPosition, getChecked);

            viewHolder.checkBox1.setChecked(false);

        }

        viewHolder.checkBox1.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);
                }
            }
        });


        convertView.setClickable(false);

        return convertView;
    }




Aucun commentaire:

Enregistrer un commentaire