java - How to restrict a user from selecting more than 10 items from a CheckBox ListView in Android -


i've searched on , cant find solution works situation. have custom checkbox adapter extending arrayadapter holds 'food' objects ('food' contains 2 attributes 1. boolean:selected , 2. string:name). when listview loads have 10 items selected default. need allow user unselect/select options limit them 10 choices.

import android.content.context; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.checkbox; import android.widget.compoundbutton; import android.widget.textview; import android.widget.toast;  import java.util.arraylist;   public class checkboxadapter extends arrayadapter<food> {      context context;     arraylist<food> foodlist;      int counter = 0;      private static class viewholder {         textview txtview;         checkbox chkbox;          public viewholder(view view) {             txtview = (textview) view.findviewbyid(r.id.food_text);             chkbox = (checkbox) view.findviewbyid(r.id.food_box);         }     }      public checkboxadapter(context context, arraylist<food> food) {         super(context, 0, food);         this.context = context;         foodlist = food;     }      @override     public view getview(int position, view convertview, viewgroup parent) {          food food = getitem(position);          view row = convertview;          viewholder viewholder = null;          if (row == null) {              layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service);             row = inflater.inflate(r.layout.food_category_row, parent, false);             viewholder = new viewholder(row);             row.settag(viewholder);              //not sure if need use - think useless...             viewholder.chkbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {                 @override                 public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {                     int getposition = (integer) buttonview.gettag();  // here position have set checkbox using settag.                     //foodlist.get(getposition).setselected(buttonview.ischecked()); // set value of checkbox maintain state.                     if(ischecked){                         counter++;                     }                     else if(ischecked){                         counter--;                     }                     if(counter > 10){                         foodlist.get(getposition).setselected(!buttonview.ischecked());                         counter--;                     }                  }             });         } else {             viewholder = (viewholder) row.gettag();          }          viewholder.chkbox.settag(position); // line important.         viewholder.txtview.settext(foodlist.get(position).getname());         viewholder.chkbox.setchecked(foodlist.get(position).isselected());          return row;      }  } 

you need that:

viewholder.chkbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {         @override         public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {             int getposition = (int) buttonview.gettag();  // here position have set checkbox using settag.             //foodlist.get(getposition).setselected(buttonview.ischecked()); // set value of checkbox maintain state.             if (ischecked) {                 counter++;             } else {                 counter--;             }             if (counter > 10) {                 buttonview.setchecked(false); // no need counter-- because listener trigger again             }          }     }); 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -