ExpandableListActivity如何在组的所有子项中选中/取消选中复选框

时间:2015-04-21 15:28:34

标签: android expandablelistview

我有一个包含群组和子节点的ExpandableListActivity。孩子们有一个复选框。 当用户点击该组时,我已覆盖了setOnGroupExpandListener。

如何检查/取消选中特定群组的每个孩子的复选框?

我已经尝试使用groupPosition获取适配器并调用getChild,但我失去了怎么做。

提前致谢

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;



import android.widget.Toast;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

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;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        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{

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


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



            }


        });

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



                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, "Branhes selected are " + appObj.dbModel.getBranchList());


                }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());



                }  



                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;

        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(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);


       return convertView;
      }

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

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




}

1 个答案:

答案 0 :(得分:0)

尝试创建一个名为checked的布尔数组。然后在getChildView中检查数组中的那个点是否为真,如果是,那么选中复选框 然后当你通过那个二维数组中的每个元素展开一个父循环时,将它们全部设置为true,然后调用notifyDataSetChanged

boolean [][] checked;

然后在展开

for(int i =0; i < checked[groupPosition].size; i++)
{
    checked[groupPosition][i] = true;
}
notifyDataSetChanged();

在getChildView中你也应该这样

cb.setChecked(checked[groupPosition][childPosition]);

可以通过将其设置为false来取消选中它们。只需记住在用户检查或取消选中按钮时更新此数组,这样当列表视图被回收时,您不会随机选中复选框。