jeudi 14 janvier 2016

How to check the state of a checkbox based on the state of another checkbox of a child in an expandable listview?

I have an ExpandableListView. Its child is a CheckBox. It works fine with checking and unchecking of the checkboxes. The CheckBox is populated with the help of a webservice using model classes. Some of the IDs of these checkboxes are similar. Now, I have to check a condition that if a CheckBox with a particular ID is unchecked, then all other checkboxes corresponding to that ID are also unchecked. How can i do this? Here is my code:

JobAlertAdapter.java

    public class JobAlertAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<CategoryJobAlert>catJobList;
    //HashMap for keeping track of our checkbox check states
    private HashMap<Integer, boolean[]> mChildCheckStates;
    private HashMap<CategoryJobAlert,List<SpecialtyJobAlert>>specJobList;
    ArrayList<ArrayList<Integer>> check_states = new ArrayList<ArrayList<Integer>>();
    private ChildViewHolder childViewHolder;
    private ExpandableListView expandableListView;
    public JobAlertAdapter(Context context, List<CategoryJobAlert> catJobList, ExpandableListView elv) {
        this.context = context;
        this.catJobList = catJobList;
        this.expandableListView=elv;
        mChildCheckStates = new HashMap<Integer, boolean[]>();
    }
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        List<SpecialtyJobAlert>specialtyList = catJobList.get(groupPosition).getCategoryspecialty();
        return specialtyList.get(childPosition);
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild,View view, ViewGroup parent) {
        final int mGroupPosition = groupPosition;
        final int mChildPosition = childPosition;

        /*CommonApi.SEL_JOB_ALERT_CAT_IDS.clear();
        CommonApi.SEL_JOB_ALERT_SPEC_IDS.clear();*/
        //  I passed a text string into an activity holding a getter/setter
        //  which I passed in through "ExpListChildItems".
        //  Here is where I call the getter to get that text
        SpecialtyJobAlert childText=(SpecialtyJobAlert) getChild(mGroupPosition, mChildPosition);

        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.category_items, null);
            childViewHolder=new ChildViewHolder();
            childViewHolder.mCheckBox = (CheckBox) view.findViewById(R.id.tvCategoriesItem);
            view.setTag(R.layout.category_items, childViewHolder);

        } else {
            childViewHolder = (ChildViewHolder) view.getTag(R.layout.category_items);
        }

        childViewHolder.mCheckBox.setText(childText.getSpecialtyName());

        /*
         * 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
        */
        childViewHolder.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 retrieve 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]
            childViewHolder.mCheckBox.setChecked(getChecked[mChildPosition]);
            System.out.print("If Group Position=" + mGroupPosition);
            System.out.print("If Child Position=" +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]
            childViewHolder.mCheckBox.setChecked(false);
        }
        if(childText.getIsChecked()) {
            childViewHolder.mCheckBox.setChecked(true);
            CommonApi.SEL_JOB_ALERT_CAT_IDS.add(catJobList.get(mGroupPosition).getCategoryID());
            CommonApi.SEL_JOB_ALERT_SPEC_IDS.add(catJobList.get(mGroupPosition).categoryspecialty.get(mChildPosition).getSpecialtyID());
        }

        childViewHolder.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);

                    CommonApi.SEL_JOB_ALERT_CAT_IDS.add(catJobList.get(mGroupPosition).getCategoryID());
                    CommonApi.SEL_JOB_ALERT_SPEC_IDS.add(catJobList.get(mGroupPosition).categoryspecialty.get(mChildPosition).getSpecialtyID());
                } else {
                    boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
                    getChecked[mChildPosition] = isChecked;
                    mChildCheckStates.put(mGroupPosition, getChecked);

                    CommonApi.SEL_JOB_ALERT_CAT_IDS.remove(catJobList.get(mGroupPosition).getCategoryID());
                    CommonApi.SEL_JOB_ALERT_SPEC_IDS.remove(catJobList.get(mGroupPosition).categoryspecialty.get(mChildPosition).getSpecialtyID());

                    /*for(int i=0;i<catJobList.size();i++) {
                        System.out.println("Category ID="+catJobList.get(i).getCategoryID());
                        for(int j=0;j<catJobList.get(i).getCategoryspecialty().size();j++){
                            System.out.println("Specialty ID="+catJobList.get(i).getCategoryspecialty().get(j).getSpecialtyID());
                            if(catJobList.get(i).getCategoryspecialty().get(j).getSpecialtyID().equals(catJobList.get(mGroupPosition).categoryspecialty.get(mChildPosition).getSpecialtyID())) {
                                //CommonApi.SEL_JOB_ALERT_SPEC_IDS.remove(catJobList.get(mGroupPosition).categoryspecialty.get(mChildPosition).getSpecialtyID());
                                childViewHolder.mCheckBox.setChecked(false);
                                break;
                            }
                        }
                    }*/
                }
            }
        });
        return view;
    }
    public final class ChildViewHolder {
        CheckBox mCheckBox;
    }
    @Override
    public int getChildrenCount(int groupPosition) {
        List<SpecialtyJobAlert> productList = catJobList.get(groupPosition).getCategoryspecialty();
        return productList.size();
    }
    @Override
    public Object getGroup(int groupPosition) {
        return catJobList.get(groupPosition);
    }

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isLastChild, View view, ViewGroup parent) {
        CategoryJobAlert headerInfo = (CategoryJobAlert) getGroup(groupPosition);
        View v = view;
        if (v== null) {
            LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v= inf.inflate(R.layout.item_job_alert, null);
        }
        expandableListView.expandGroup(groupPosition);
        TextView jobAlertItem = (TextView) v.findViewById(R.id.jobAlertItem);
        jobAlertItem.setText(headerInfo.getCategoryName().trim());

        return v;
    }

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

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

Any help would be appreciated. Thank you!




Aucun commentaire:

Enregistrer un commentaire