Android ExpandableListView组视图复选框取消选中其他组视图

Android ExpandableListView组视图复选框取消选中其他组视图,android,checkbox,expandablelistview,expandablelistadapter,Android,Checkbox,Expandablelistview,Expandablelistadapter,我的活动中有以下适配器。我在组视图上设置了一个复选框,当触摸该复选框时,将选中/取消选中子视图(也有一个复选框) 复选框的状态(true或false)存储在数据库中,并最初设置为true/checked。复选框设置为“最初选中” 当我触摸GroupCheckbox时,会出现一些奇怪的行为,它还会取消选中不属于已触摸的GroupView的复选框 例如,如果我有5个GroupView,每个GroupView都有5个子项。 如果我取消选中第一个组视图复选框,它将取消选中其所有子项,但也取消选中第三个组

我的
活动中有以下适配器。我在
组视图
上设置了一个
复选框
,当触摸该复选框时,将选中/取消选中子视图(也有一个
复选框

复选框的状态(true或false)
存储在数据库中,并最初设置为true/checked。复选框设置为“最初选中”

当我触摸
GroupCheckbox
时,会出现一些奇怪的行为,它还会取消选中不属于已触摸的
GroupView
的复选框

例如,如果我有5个
GroupView
,每个
GroupView
都有5个子项。 如果我取消选中第一个
组视图
复选框
,它将取消选中其所有子项,但也取消选中第三个
组视图
复选框及其子项

你知道为什么吗

[编辑1]

我在开始悬赏时删除了以前的代码片段。由于Ankit Kumar在
getGroupView
方法中帮助我使用了Viewholder模式,代码与原来的有所不同。当只触摸一个复选框时,它仍然取消选中多个groupview复选框。后备数据库将复选框的所有状态都选中为
true

视图似乎与数据库不同步。我已经登出声明,证明所有复选框的状态都是正确的

有没有办法只让用户点击的复选框取消选中,而不影响其他复选框

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;
        }
    }
}
公共类解释列表扩展了ExpandableListActivity{
字符串arrGroupelements[];
字符串元素[][];
私有静态最终字符串标记=ExpList.class.getSimpleName();
显示度量;
整数宽度;
可扩展列表视图解释列表;
RROnCallApplication appObj;
公司;
按钮主菜单;
EXP适配器;
国际公司账户;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.e(标记为“oncreate”);
appObj=(RROnCallApplication)getApplication();
mainMenu=(按钮)findViewById(R.id.buttonmainmenu);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
意图i=新意图(ExpList.this,MenuActivity2.class);
星触觉(i);
}
});
试一试{
companys=appObj.dbModel.queryalFromCompanyBranch();
arrGroupelements=新字符串[companys.getCount()];
Log.e(标记“companys count=“+companys.getCount());
arrChildelements=新字符串[arrGroupelements.length][20];
if(companys!=null&&companys.getCount()>0){
if(companys.moveToFirst()){
int i=0;
做{
arrGroupelements[i]=companys.getString(companys.getColumnIndex(DBModel.C_COMPANYBRANCH_NAME));
Log.e(标记,“arrGroupelements[“+i+”]=“+arrGroupelements[i]);
int compID=appObj.dbModel.getCompanyidFromName(arrGroupelements[i]);
Log.e(标签,“compID=”+compID);
字符串[]branchesArr=appObj.dbModel.getBranchNamesfromCompanyId(compID);
Log.e(标签,“branchesArr.length=“+branchesArr.length”);
对于(int h=0;hprivate 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;
}
ExpandableListView mExpandableListView = (ExpandableListView) parent;
mExpandableListView.expandGroup(groupPosition);