Expandable Listview - Checking CheckBox in group and child

169 views Asked by At

I am in developing an expandable listview with checkbox in group and child. My Goal is when checkbox in group is checked, so the checkbox in child is checked too. But now I have problem: if the checkbox in group is checked, the child will be checked if I scroll the list after I see all child..

Can anybody help me to solve this problem? Thanks in advance.

This is My Adapter:

public class AdapterHistoryExpandable extends BaseExpandableListAdapter {

    private Context context;
    private boolean checkAll = false;
    private ArrayList<GroupItemsInfo> teamName;
    public int isGroupChecked = 0;
    HistoryHeaderActivity main;
    TotalListener mListener;

    class ViewHolder {
        public CheckBox checkChild, checkGroup;
        public TextView date, h_ps_id, h_ps_name, h_dokname, h_item, h_qty, h_service, heading;
    }

    public AdapterHistoryExpandable(HistoryHeaderActivity main, ArrayList<GroupItemsInfo> deptList) {
        this.main = main;
        this.teamName = deptList;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
    }

    @Override
    public int getGroupCount() {
        return teamName.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        ArrayList<ChildItemsInfo> productList = teamName.get(groupPosition).getSongName();
        return productList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return teamName.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        ArrayList<ChildItemsInfo> productList = teamName.get(groupPosition).getSongName();
        return productList.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    public void setmListener(TotalListener mListener) {
        this.mListener = mListener;
    }

    public void setmGroupList(ArrayList<GroupItemsInfo> mGroupList) {
        this.teamName = mGroupList;
    }

    @Override
    public View getGroupView(final int groupPosition, final boolean isExpanded, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        final GroupItemsInfo headerInfo = (GroupItemsInfo) getGroup(groupPosition);
        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) main.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.group_items, null);
            holder.checkGroup = (CheckBox) convertView.findViewById(R.id.checkList);
            holder.checkGroup.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (!isExpanded)
                        mListener.expandGroupEvent(groupPosition, isExpanded);
                    int getPosition = (Integer) buttonView.getTag();
                    headerInfo.setSelected(isChecked);

                    if (!isChecked) {
                        headerInfo.setSelected(false);
                    }
                    Intent intent = new Intent(context, AdapterHistoryExpandable.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    context.startActivity(intent);
                }
            });
            convertView.setTag(holder);
            convertView.setTag(R.id.checkList, holder.checkGroup);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.heading = (TextView) convertView.findViewById(R.id.heading);
        holder.checkGroup.setTag(groupPosition);
        holder.checkGroup.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                main.groupInfos.get(groupPosition).setSelected(buttonView.isChecked());
            }
        });
        holder.checkGroup.setChecked(main.groupInfos.get(groupPosition).isSelected());

        holder.heading.setText(headerInfo.getName().trim());
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final ChildItemsInfo detailInfo = (ChildItemsInfo) getChild(groupPosition, childPosition);
        final ViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) main.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_history, null);
            holder = new ViewHolder();
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.date = (TextView) convertView.findViewById(R.id.date_);
        holder.h_ps_id = (TextView) convertView.findViewById(R.id.patient_id);
        holder.h_ps_name = (TextView) convertView.findViewById(R.id.patient_);
        holder.h_dokname = (TextView) convertView.findViewById(R.id.doctor_);
        holder.h_item = (TextView) convertView.findViewById(R.id.item_);
        holder.h_qty = (TextView) convertView.findViewById(R.id.qty_);
        holder.h_service = (TextView) convertView.findViewById(R.id.servicetype_);
        holder.checkChild = (CheckBox) convertView.findViewById(R.id.checkBox);

        ModelChecking add = new ModelChecking();
        if (main.groupInfos.get(groupPosition).isSelected()) {
            holder.checkChild.setChecked(true);
            if (holder.checkChild.isChecked()) {
                add.setIDItem(detailInfo.getHitem());
                add.setQtyItem(detailInfo.getHqty());
                main.checkingItem.add(add);
            }
        } else {
            holder.checkChild.setChecked(false);
            main.checkingItem.clear();

        }

        holder.h_ps_id.setText(detailInfo.getHp_id().trim());
        holder.date.setText(detailInfo.getDate().trim());
        holder.h_ps_name.setText(detailInfo.getHp_name().trim());
        holder.h_dokname.setText(detailInfo.getHd_name().trim());
        holder.h_item.setText(detailInfo.getHitem().trim());
        holder.h_qty.setText(detailInfo.getHqty().trim());
        holder.h_service.setText(detailInfo.getHservice().trim());

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {

    }

    @Override
    public long getCombinedChildId(long groupId, long childId) {
        return 0;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return 0;
    }
}
0

There are 0 answers