Advertisement
Guest User

Multi_checkboxes_Ans

a guest
Dec 8th, 2011
2,740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. In activity called  AttendanceActivity.class,
  2.  
  3. //methos called in onCreate()
  4. private void onClickSetAllAttendance() {
  5.         selectAll.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  6.  
  7.             @Override
  8.             public void onCheckedChanged(CompoundButton buttonView,
  9.                     boolean isChecked) {
  10.                 if (selectAll.isChecked()) {
  11.                     InteractiveArrayAdapter.check_all_status = 1;
  12.                     listAdapter.notifyDataSetChanged();
  13.                 } else {
  14.                     InteractiveArrayAdapter.check_all_status = 0;
  15.                     listAdapter.notifyDataSetChanged();
  16.                 }
  17.             }
  18.         });
  19.  
  20.     }
  21.  
  22.  
  23. My adapter class---
  24. -------------------------
  25. /*
  26.                  * Here in this adapter I am taking "SelectAll" chekbox's status as check_all_status. It will assign according to conditions
  27.                  * In getview() i am calling a function called mCheckSelectedSize()  to check whether all checkboxes are selected or not
  28.                  * according to returned value from mCheckSelectedSize() within onCheckedChanged() I am cheking condition and assigning the values to
  29.                  * check_all_status and check / uncheck to SelectAll checkbox
  30.                  */
  31.  
  32. public class InteractiveArrayAdapter extends ArrayAdapter<Model> {
  33.  
  34.     private final List<Model> listModel;
  35.     private final Activity context;
  36.     private TextView text;
  37.     private CheckBox checkbox, selectAll;
  38.     public static int check_all_status= -1;
  39.  
  40.     public InteractiveArrayAdapter(Activity context, List<Model> list, CheckBox selectAll){
  41.         super(context, R.layout.list_items_attendance_payment, list);
  42.         this.context = context;
  43.         this.listModel = list;
  44.         this.selectAll= selectAll;
  45.     }
  46.  
  47.     @Override
  48.     public View getView( int position, View convertView, ViewGroup parent) {
  49.         View view = convertView;
  50.         if (convertView == null) {
  51.             LayoutInflater inflator = context.getLayoutInflater();
  52.             view = inflator.inflate(R.layout.list_items_attendance_payment, null);
  53.         }
  54.  
  55.         final Model obj = listModel.get(position);
  56.         if(obj!=null){
  57.             text = (TextView) view.findViewById(R.id.name);
  58.             checkbox = (CheckBox) view.findViewById(R.id.check);
  59.            
  60.             if(text!=null){
  61.                 text.setText(listModel.get(position).getName()+" (No :"+listModel.get(position).getmNumber()+")");
  62.             }
  63.             if(checkbox!=null){
  64.                
  65.                
  66.                 checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  67.                    
  68.                     @Override
  69.                     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  70.                         obj.setSelected(isChecked);
  71.                        
  72.                         boolean isAllCheckSelected = (mCheckSelectedSize()==listModel.size());
  73.                         if((isAllCheckSelected && isChecked)||(check_all_status == 1 && isChecked)){
  74.                             selectAll.setChecked(true);
  75.                             check_all_status=1;
  76.                         }
  77.                         else if(mCheckSelectedSize()==0||check_all_status ==1){
  78.                             selectAll.setChecked(false);
  79.                             check_all_status=-1;
  80.                         }
  81.                        
  82.                     }
  83.  
  84.                     private int mCheckSelectedSize() {
  85.                             int k=0;
  86.  
  87.                             for (int i = 0; i < listModel.size(); i++) {
  88.                                                                
  89.                                 if(listModel.get(i).getSelected()){
  90.                                     k ++;
  91.                                 }
  92.                             }
  93.                             return k;
  94.                         }
  95.                 });
  96.                
  97.                
  98.                 if (check_all_status==1 ) {
  99.                     checkbox.setChecked(true);
  100.                     obj.setSelected(true);
  101.                 }
  102.                 else if(check_all_status==0){
  103.                     listModel.get(position).setSelected(false);
  104.                     checkbox.setChecked(false);
  105.                     obj.setSelected(false);
  106.                    
  107.                 }
  108.                 else
  109.                     checkbox.setChecked(listModel.get(position).getSelected());
  110.             }
  111.         }
  112.         return view;
  113.     }
  114. }
  115.  
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement