In activity called AttendanceActivity.class,
//methos called in onCreate()
private void onClickSetAllAttendance() {
selectAll.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (selectAll.isChecked()) {
InteractiveArrayAdapter.check_all_status = 1;
listAdapter.notifyDataSetChanged();
} else {
InteractiveArrayAdapter.check_all_status = 0;
listAdapter.notifyDataSetChanged();
}
}
});
}
My adapter class---
-------------------------
/*
* Here in this adapter I am taking "SelectAll" chekbox's status as check_all_status. It will assign according to conditions
* In getview() i am calling a function called mCheckSelectedSize() to check whether all checkboxes are selected or not
* according to returned value from mCheckSelectedSize() within onCheckedChanged() I am cheking condition and assigning the values to
* check_all_status and check / uncheck to SelectAll checkbox
*/
public class InteractiveArrayAdapter extends ArrayAdapter<Model> {
private final List<Model> listModel;
private final Activity context;
private TextView text;
private CheckBox checkbox, selectAll;
public static int check_all_status= -1;
public InteractiveArrayAdapter(Activity context, List<Model> list, CheckBox selectAll){
super(context, R.layout.list_items_attendance_payment, list);
this.context = context;
this.listModel = list;
this.selectAll= selectAll;
}
@Override
public View getView( int position, View convertView, ViewGroup parent) {
View view = convertView;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.list_items_attendance_payment, null);
}
final Model obj = listModel.get(position);
if(obj!=null){
text = (TextView) view.findViewById(R.id.name);
checkbox = (CheckBox) view.findViewById(R.id.check);
if(text!=null){
text.setText(listModel.get(position).getName()+" (No :"+listModel.get(position).getmNumber()+")");
}
if(checkbox!=null){
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
obj.setSelected(isChecked);
boolean isAllCheckSelected = (mCheckSelectedSize()==listModel.size());
if((isAllCheckSelected && isChecked)||(check_all_status == 1 && isChecked)){
selectAll.setChecked(true);
check_all_status=1;
}
else if(mCheckSelectedSize()==0||check_all_status ==1){
selectAll.setChecked(false);
check_all_status=-1;
}
}
private int mCheckSelectedSize() {
int k=0;
for (int i = 0; i < listModel.size(); i++) {
if(listModel.get(i).getSelected()){
k ++;
}
}
return k;
}
});
if (check_all_status==1 ) {
checkbox.setChecked(true);
obj.setSelected(true);
}
else if(check_all_status==0){
listModel.get(position).setSelected(false);
checkbox.setChecked(false);
obj.setSelected(false);
}
else
checkbox.setChecked(listModel.get(position).getSelected());
}
}
return view;
}
}