ExpandableListView groupView checkbox unticking other groupviews
Asked Answered
M

3

12

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?

[Edit 1]

I've deleted previous code snippets as I'm starting a bounty. The code has changed from the original as Ankit Kumar has helped me with the Viewholder patter in getGroupView method. It still unchecks multiple groupview checkboxes when only one checkbox is touched. the backing DB has all the states of the checkboxes checked as true to start with.

It would seem that the view is out of sync with the DB. I've logged out statements that prove the state of all checkboxes are true to start with.

Is there a way to only make the checkbox the user touches uncheck without effecting the other checkboxes?

public class ExpList extends ExpandableListActivity {

    String arrGroupelements[];
    String arrChildelements[][];
    private static final String TAG = ExpList.class.getSimpleName();
    DisplayMetrics metrics;
    int width;
    ExpandableListView expList;

    RROnCallApplication appObj;
    Cursor companies;
    Button mainMenu;

    ExpAdapter adapter;
    int companyCount;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.e(TAG, "oncreate");

        appObj = (RROnCallApplication) getApplication();

        mainMenu = (Button)findViewById(R.id.buttonmainmenu);
        mainMenu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(ExpList.this, MenuActivity2.class);
                startActivity(i);
            }
        });

        try {
            companies = appObj.dbModel.queryAllFromCompanyBranch();
            arrGroupelements = new String[companies.getCount()];
            Log.e(TAG, "companies count  = " + companies.getCount());

            arrChildelements = new String[arrGroupelements.length][20];

            if(companies != null && companies.getCount() > 0) {
                if(companies.moveToFirst()) {

                    int i = 0;

                     do {
                        arrGroupelements[i] = companies.getString(companies.getColumnIndex(DBModel.C_COMPANYBRANCH_NAME));
                        Log.e(TAG, "arrGroupelements[" + i +"] = " + arrGroupelements[i]);

                        int compID = appObj.dbModel.getCompanyidFromName(arrGroupelements[i]);
                        Log.e(TAG, "compID = " + compID);

                        String[] branchesArr = appObj.dbModel.getBranchNamesfromCompanyId(compID);
                        Log.e(TAG, "branchesArr length = " + branchesArr.length);

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

                        i++;
                    }while(companies.moveToNext());

                    Log.e(TAG, "arrGroupelements size = " + arrGroupelements.length);

                }//end of moveToFirst
            }
        }
        catch(Exception e) {
            Toast.makeText(this, "There was a problem downloading companies and branches", Toast.LENGTH_LONG).show();
            Log.e(TAG, "********Exception = " + e.toString());
        }
        finally {
            companyCount = companies.getCount();
            companies.close();
        }

        expList = getExpandableListView();
        metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        width = metrics.widthPixels;
        //this code for adjusting the group indicator into right side of the view
        expList.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));
        expList.setAdapter(new ExpAdapter(this));

        for(int h = 0; h < companyCount; h++){
            expList.expandGroup(h);
        }

        expList.setOnGroupExpandListener(new OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                Log.e("onGroupExpand", "OK");
                Log.e("groupPosition", " " + groupPosition);
            }
        });

    //      expList.setOnGroupClickListener(new OnGroupClickListener() {
    //          
    //          @Override
    //          public boolean onGroupClick(ExpandableListView parent, View v,
    //                  int groupPosition, long id) {
    //
    //
    //              Log.e(TAG, "groupPosition in onGroupClickListener = " + groupPosition);
    //              
    //              int count = 0;
    //              
    //                for (int i = 0; i < arrChildelements[groupPosition].length; i++){
    //                    
    //                    if(arrChildelements[groupPosition][i] != null){
    //                    
    //                  count += arrChildelements[groupPosition][i] != null ? 1 : 0;
    //                  
    //                  Log.e("TAG", "child count in onGroupClickListener = " + count);
    //                  Log.e(TAG, "arrChildelements[groupPosition][i] = " + arrChildelements[groupPosition][i]);
    //                  
    //                  int branchID = appObj.dbModel.getBranchIdFromName(arrChildelements[groupPosition][i]);
    //                  
    //                  appObj.dbModel.updateBranchSelectedStatus(String.valueOf(branchID), "N");
    //                  
    //                  
    //              
    //                 }//end of if
    //                    
    //                }//end of for loop
    //                
    //                
    //              
    //              return false;
    //          }
    //      });

        expList.setOnGroupCollapseListener(new OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                Log.e("onGroupCollapse", "OK");
            }
        });

        expList.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

                Log.e("OnChildClickListener", "OK Group = " + groupPosition + " child = " + childPosition);

                TextView tvBranchName = (TextView) v.findViewById(R.id.tvPlayerName);
                String branchName = tvBranchName.getText().toString();

                Log.e(TAG, "branch name = " + branchName);

                int branchID = appObj.dbModel.getBranchIdFromName(branchName);

                Log.e(TAG, "branch ID = " + branchID);

                String companyName = arrGroupelements[groupPosition];
                Log.e(TAG, "**********CompanyName = " + companyName);

                final CheckBox cb = ((CheckBox)v.findViewById(R.id.checkbox));

                if(cb.isChecked() == true) {
                    Log.e(TAG, "checkBox is true but setting it to false now" );
                    cb.setChecked(false);
                    appObj.dbModel.updateBranchSelectedStatus(String.valueOf(branchID), "N");
                    Log.e(TAG, "just called updateBranchSelectedStatus with values " + String.valueOf(branchID) + " " + "N");

                    Log.e(TAG, "Branches selected are " + appObj.dbModel.getBranchList());

                    if(appObj.dbModel.isCompanySelected(companyName) == false){
                        //set companySeelcted to false
                        appObj.dbModel.updateCompanySelectedStatus(companyName, "N");
                        Log.e(TAG, "Setting company to no longer selected as no branches are selected for " + companyName);
                    }
                }
                else {
                    Log.e(TAG, "checkBox is false but setting it to true");
                    cb.setChecked(true);
                    appObj.dbModel.updateBranchSelectedStatus(String.valueOf(branchID), "Y");
                    Log.e(TAG, "just called updateBranchSelectedStatus with values " + String.valueOf(branchID) + " " + "Y");

                    Log.e(TAG, "Branhes selected are " + appObj.dbModel.getBranchList());

                    //set company to selected
                    appObj.dbModel.updateCompanySelectedStatus(companyName, "Y");
                }  

                return false;
            }
        });
    }//end of onCreate

    @Override
    public void onBackPressed() {
        super.onBackPressed();

        Intent i = new Intent(this, OnCallMenuActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
    }

    public int GetDipsFromPixel(float pixels) {
        // Get the screen's density scale
        final float scale = getResources().getDisplayMetrics().density;

        // Convert the dps to pixels, based on density scale
        return (int) (pixels * scale + 0.5f);
    }

    public class ExpAdapter extends BaseExpandableListAdapter {

        private Context myContext;

        class ViewHolder {
            public TextView groupName;
            public CheckBox groupCheckBox;
        }

        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;
            Log.e(TAG, "getChildrenCount");

            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() {
            Log.e(TAG, "getGroupCount");
            return arrGroupelements.length;
        }

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

        ViewHolder viewHolder;

        @Override
        public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            Log.e(TAG, "getGroupView");
            viewHolder = new ViewHolder();

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

                viewHolder.groupName = (TextView) convertView.findViewById(R.id.tvGroupName);
                viewHolder.groupCheckBox = (CheckBox) convertView.findViewById(R.id.groupcheckbox);
                convertView.setTag(viewHolder);
            } else viewHolder = (ViewHolder) convertView.getTag();

            final ViewHolder holder = viewHolder;
            holder.groupName.setText(arrGroupelements[groupPosition]);

            holder.groupCheckBox.setTag(groupPosition);
            holder.groupCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

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

                    int pos = (Integer) holder.groupCheckBox.getTag();

                    Log.e(TAG, "oncheckChanged has fired at position " + pos);
                    int yCount = 0;
                    Cursor c = appObj.dbModel.queryAllFromCompanyBranch();
                    c.moveToFirst();

                    do {
                        String str = c.getString(c.getColumnIndex(DBModel.C_COMPANYBRANCH_SELECTED));

                        if(str.equalsIgnoreCase("Y")) {
                            yCount++;
                        }
                    } while(c.moveToNext());

                    Log.e(TAG, "yCount before = " + yCount);

                    if(isChecked == true) {
                        Log.e(TAG, "checkBox true");

                        int count = 0;

                        for (int i = 0; i < getChildrenCount(pos); 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[pos], "Y");
                            }//end of if
                        }//end of for loop
                    }
                    else if(isChecked == false) {
                        Log.e(TAG, "checkBox false");

                        int count = 0;

                        for (int i = 0; i < getChildrenCount(pos); 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[pos], "N");
                                Log.e(TAG, "Setting company to no longer selected as no branches are selected for " + arrGroupelements[pos]);
                            }//end of if
                        }//end of for loop
                    } 

                    int yCount2 = 0;
                    Cursor c2 = appObj.dbModel.queryAllFromCompanyBranch();
                    c2.moveToFirst();

                    do {
                        String str2 = c2.getString(c2.getColumnIndex(DBModel.C_COMPANYBRANCH_SELECTED));

                        if(str2.equalsIgnoreCase("Y")){
                            yCount2++;
                        }

                    } while(c2.moveToNext());

                    Log.e(TAG, "yCount2 after = " + yCount2);

                    notifyDataSetChanged();
                }
            });

            ExpandableListView mExpandableListView = (ExpandableListView) parent;
            mExpandableListView.expandGroup(groupPosition);
            return convertView;
        }

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

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }
}
Mind answered 28/4, 2015 at 12:56 Comment(11)
Use ViewHolder for initializing and using your Views in AdapterMaterse
are you facing issue while scrolling?Klondike
@AnkitKumar A viewHolder speeds up the responsiveness of the adapter by not having to look up the objects but does this explain why when i touch one GroupView checkbox another groupview is selected as well?Mind
yes... getview method will keep changing view everytime.. using viewholder and making it final for a particular position, it wont effect/affect other getviews.Materse
@NoName no scrolling is fine, it is only when i touch a groupview checkbox, some of the other groupview checkboxes are affected as wellMind
@AnkitKumar Hi I'm not sure if i've done it correctly but it still doesn't work. I've posted the changes i have made in [Edit1]. is that what you meant?Mind
@Mind .. hi just see my answer.. I face same problem and solved it like I wrote answer.Materse
Um, this is a wall of code. Moreover, it is many times more complicated than it needs to be, because it ties into a whole bunch of stuff tied to the business logic of your app. Create a separate app where you attempt to reproduce the problem, with no database and minimum use of model objects. If you can reproduce the problem, that would be better sample code to use in the question. If you cannot reproduce the problem, you have a working solution and need to move your existing code more towards what's in that solution.Sven
This is a lot of code and a lot of places for things to go wrong. With that said, what I have done with RadioButtons (same concept) is to store an id of each child in a HashMap<String, Boolean> and whether it is checked or not. I also had to set them all to unchecked each time through then check if the id is in the list in getChildView() and set it accordingly.Bluet
@Mind You need to save an instance of their state and store it in your Bundle args(). Save it in onSavedInstancedState() and retrieve it from onViewStateRestored() and also attach a listener to the viewGroup so that when a selection is saved It saves the ID of the child and returns it to the parent activity. Can you post a pic? I need to visualize it if you want any codeDerry
@Mind make your view holder final all the time you are not making it final for the first time. Create holder object for getchildview() as well and declare it final. also attach the pic of your activity so that we have better idea what is happening.Righthander
S
6

I haven't gone through your problem but based on the same, I have created a sample project for you, please try with it and i am sure that it will help you to resolve your issue.

Expandable Checklist

Output:-

enter image description here

Slink answered 13/5, 2015 at 8:19 Comment(0)
M
2
private Context myContext;

        class ViewHolder {
        public TextView groupName;
        public CheckBox groupCheckBox;

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

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

    viewHolder.groupName = (TextView) convertView.findViewById(R.id.tvGroupName);
    viewHolder.groupCheckBox = (CheckBox) convertView.findViewById(R.id.groupcheckbox);
    convertView.setTag(viewHolder);
  } else viewHolder = (ViewHolder) convertView.getTag();

   final ViewHolder holder = viewHolder;
   holder.groupName.setText(arrGroupelements[groupPosition]);

   holder.groupCheckBox.setTag(groupPosition);
   holder.groupCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          // Write Your required code here//
        }
  // your other code//
   return convertView;
}

do this part on Activity/Fragment where your setting adapter

ExpandableListView mExpandableListView = (ExpandableListView) parent;
mExpandableListView.expandGroup(groupPosition);
Materse answered 28/4, 2015 at 13:46 Comment(7)
i'm not sure where to put the last part ExpandableListView mExpandableListView = (ExpandableListView) parent; mExpandableListView.expandGroup(groupPosition);?Mind
where your setting ExpAdapter to your listview??Materse
Yeah.. U need to put that part in Activity or Fragment which is using your expandable ListViewMaterse
the following line is in my oncreate expList.setAdapter(new ExpAdapter(this)); so i can use the list expList, but how do i call expandGroup(groupPostion) on this object when i have no groupPosition?Mind
expandableListView.expandGroup(0); like thisMaterse
if u know position of group to expand, use posiotion... if u dont know then write a method in your adapter and access it in your activityMaterse
I've got the list to fully expand by using the code in edit3 but i'm afraid multiple checkboxes are still getting checked from touching one groupview checkboxMind
R
1

declare your view holder final all the time. You are not declaring it final for the first time. Create holder object for getchildview() as well and declare it final.

if it doesn't work then please attach picture if your running activity so that we can have better idea whats going on.

Righthander answered 12/5, 2015 at 16:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.