mardi 28 avril 2015

ExpandableListView groupView checkbox unticking other groupviews

I have the following adapter in my Activity. I have set a checkBox on the groupView that when touched will check/uncheck the child views(which also have a checkbox).

The state of the checkBox(true or false) is stored in the DB and initially set to true/checked. The checkboxes are set to checked initially.

There is some strange behaviour when i touch a GroupCheckbox, it also unchecks the checkbox that are not part of the groupview that has been touched.

so for example if i have 5 groupViews and each GroupView has 5 children. if I uncheck the 1st groupView checkbox, it unchecks all its children but also the 3rd groupview checkbox and its children.

Are there any ideas why?

public class ExpAdapter extends BaseExpandableListAdapter {

        private Context myContext;

        public ExpAdapter(Context context) {
            myContext = context;
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return null;
        }

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

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

            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.child_row, null);
            }

            TextView tvPlayerName = (TextView) convertView.findViewById(R.id.tvPlayerName);
            tvPlayerName.setText(arrChildelements[groupPosition][childPosition]);

            CheckBox cb = (CheckBox)convertView.findViewById(R.id.checkbox);
            int branchID = appObj.dbModel.getBranchIdFromName(arrChildelements[groupPosition][childPosition]);
            Log.e(TAG, "inside getchildView and branchID = " + branchID);
            boolean isBranchSelected = appObj.dbModel.isBranchSelected(String.valueOf(branchID));
            Log.e(TAG, "isBranchSelected = " + isBranchSelected);

            if(isBranchSelected == true){

                cb.setChecked(true);
                Log.e(TAG, "inside getchildView and cb.setChecked(true)");

            }else{

                cb.setChecked(false);
                Log.e(TAG, ")))))))))))))))))))))))))))))))))))))))inside getchildView and cb.setChecked(false)");
            }




            return convertView;
        }

      @Override
      public int getChildrenCount(int groupPosition) {
       //return arrChildelements[groupPosition].length;

     int count = 0;
      for (int i = 0; i < arrChildelements[groupPosition].length; i++)
        count += arrChildelements[groupPosition][i] != null ? 1 : 0;
      return count;
      }

      @Override
      public Object getGroup(int groupPosition) {
       return null;
      }

      @Override
      public int getGroupCount() {
       return arrGroupelements.length;
      }

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

      @Override
      public View getGroupView(final int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {

       if (convertView == null) {
        LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.group_row, null);
       }



       TextView tvGroupName = (TextView) convertView.findViewById(R.id.tvGroupName);
       tvGroupName.setText(arrGroupelements[groupPosition]);

     ExpandableListView mExpandableListView = (ExpandableListView) parent;
     mExpandableListView.expandGroup(groupPosition);


     final CheckBox cb = ((CheckBox)convertView.findViewById(R.id.groupcheckbox));
     cb.setTag(groupPosition);
      cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

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

                int pos = (Integer) cb.getTag();


                if(isChecked == true){

                    Log.e(TAG, "checkBox true");

                    int count = 0;

                      for (int i = 0; i < arrChildelements[pos].length; i++){

                          if(arrChildelements[pos][i] != null){

                        count += arrChildelements[pos][i] != null ? 1 : 0;

                        Log.e("TAG", "child count in onGroupClickListener = " + count);
                        Log.e(TAG, "arrChildelements[groupPosition][i] = " + arrChildelements[pos][i]);

                        int branchID = appObj.dbModel.getBranchIdFromName(arrChildelements[pos][i]);

                        appObj.dbModel.updateBranchSelectedStatus(String.valueOf(branchID), "Y");
                        appObj.dbModel.updateCompanySelectedStatus(arrGroupelements[groupPosition], "Y");


                        notifyDataSetChanged();



                       }//end of if

                      }//end of for loop



                }else if(isChecked == false){

                    Log.e(TAG, "checkBox false");

                    int count = 0;

                      for (int i = 0; i < arrChildelements[pos].length; i++){

                          if(arrChildelements[pos][i] != null){

                        count += arrChildelements[pos][i] != null ? 1 : 0;

                        Log.e("TAG", "child count in onGroupClickListener = " + count);
                        Log.e(TAG, "arrChildelements[groupPosition][i] = " + arrChildelements[pos][i]);

                        int branchID = appObj.dbModel.getBranchIdFromName(arrChildelements[pos][i]);

                        appObj.dbModel.updateBranchSelectedStatus(String.valueOf(branchID), "N");
                        appObj.dbModel.updateCompanySelectedStatus(arrGroupelements[groupPosition], "N");
                        Log.e(TAG, "Setting company to no longer selected as no branches are selected for " + arrGroupelements[groupPosition]);



                        notifyDataSetChanged();

                       }//end of if

                      }//end of for loop

                }  

            }
        });




       return convertView;
      }

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

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




Aucun commentaire:

Enregistrer un commentaire