listview - How to determine the number of checkboxes that can be checked in Android? -


hi have checkboxes inside listview have 8 choices, , want user able check four, lock checkboxes unless unchecks one.

this main:

public class ch2 extends activity implements onitemclicklistener{     listview lv;     arrayadapter<model> adapter;     list<model> list = new arraylist<model>();     int num;      public void oncreate(bundle icicle) {         super.oncreate(icicle);         setcontentview(r.layout.activity_ch2);          lv = (listview) findviewbyid(r.id.listview2);         adapter = new cusch2(this,getmodel());         lv.setadapter(adapter);         lv.setonitemclicklistener(this);     }      @override     public void onitemclick(adapterview<?> arg0, view v, int position, long arg3) {         textview label = (textview) v.gettag(r.id.label);         final checkbox checkbox = (checkbox) v.gettag(r.id.check);         checkbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener(){              @override             public void oncheckedchanged(compoundbutton arg0, boolean ischecked) {                 if (ischecked){                      toast.maketext(getapplicationcontext(),"hi",toast.length_long);                       // disable checkbox                 }             }         });         toast.maketext(v.getcontext(), label.gettext().tostring()+" "+ischeckedornot(checkbox), toast.length_long).show();      }      private string ischeckedornot(checkbox checkbox) {         if(checkbox.ischecked()) {             num++;             if (num == 4) {                 checkbox.setenabled(false);              }             return "is checked";          } else             return "is not checked";     }       private list<model> getmodel() {         list.add(new model("a"));         list.add(new model("b"));         list.add(new model("c"));         list.add(new model("d"));         list.add(new model("e"));         list.add(new model("f"));         list.add(new model("m"));         list.add(new model("o"));         return list;     } 

and arrayadapter

public class cusch2  extends arrayadapter<model> {      private final list<model> list;     private final activity context;     boolean checkall_flag = false;     boolean checkitem_flag = false;       public cusch2(activity context, list<model> list) {         super(context, r.layout.row, list);         this.context = context;         this.list = list;     }      static class viewholder {         protected textview text;         protected checkbox checkbox;       }      @override     public view getview(final int position, view convertview, viewgroup parent) {        viewholder viewholder = null;     if (convertview == null) {         layoutinflater inflator = context.getlayoutinflater();         convertview = inflator.inflate(r.layout.row, null);         viewholder = new viewholder();         viewholder.text = (textview) convertview.findviewbyid(r.id.label);         viewholder.checkbox = (checkbox) convertview.findviewbyid(r.id.check);         final viewholder finalviewholder = viewholder;         final viewholder finalviewholder1 = viewholder;         final viewholder finalviewholder2 = viewholder;         viewholder.checkbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {              @override             public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {                 int getposition = (integer) buttonview.gettag();  // here position have set checkbox using settag.                 list.get(getposition).setselected(buttonview.ischecked()); // set value of checkbox maintain state.              }         });         convertview.settag(viewholder);         convertview.settag(r.id.label, viewholder.text);         convertview.settag(r.id.check, viewholder.checkbox);     } else {         viewholder = (viewholder) convertview.gettag();     }     viewholder.checkbox.settag(position); // line important.      viewholder.text.settext(list.get(position).getname());     viewholder.checkbox.setchecked(list.get(position).isselected());      return convertview; } } 

and model

public class model {      private string name;     private boolean selected;      public model(string name) {         this.name = name;     }      public string getname() {         return name;     }      public boolean isselected() {         return selected;     }      public void setselected(boolean selected) {         this.selected = selected;     } 

of course have row.xml file

as say, want make user able check @ 4 boxes.

the solution tried:

    @override     public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {         int getposition = (integer) buttonview.gettag();  // here position have set checkbox using settag.         list.get(getposition).setselected(buttonview.ischecked()); // set value of checkbox maintain state.         if(!ischecked){             num++;              if(num>3){                finalviewholder.checkbox.setchecked(false);}             if(!ischecked){                 num--;                 if(num<3){                     finalviewholder.checkbox.setchecked(true);}             }           }     } }); 

it's working stop check, when scroll list, lose checked.

edit 2:

      viewholder.checkbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {              @override             public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {                 int getposition = (integer) buttonview.gettag();  // here position have set checkbox using settag.               list.get(getposition).setselected(buttonview.ischecked()); // set value of checkbox maintain state.                 toast toast=  toast.maketext(getcontext(), string.valueof(getposition), toast.length_short);                  if (ischecked) {                     num++;                     l.add(string.valueof(getposition));                     if(l.size() > 3){                             finalviewholder.checkbox.setchecked(false);                     }                              toast.setgravity(gravity.top|gravity.center_horizontal, 0, 0);                     toast.show();                 }                      else if(!ischecked){                     finalviewholder.checkbox.setchecked(true);                     }              }          }); 

also when scroll loss checked

edit 2:

   viewholder.checkbox.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view v) {                  int getposition = (integer) v.gettag();  // here position have set checkbox using settag.                 list.get(getposition).setselected(finalviewholder.checkbox.ischecked()); // set value of checkbox maintain state.                 toast toast=  toast.maketext(getcontext(), string.valueof(getposition), toast.length_short);                  if (finalviewholder.checkbox.ischecked()) {                      l.add(string.valueof(getposition));                     toast.setgravity(gravity.top | gravity.center_horizontal, 0, 0);                     toast.show();                      if (l.size() > 4) {                         finalviewholder.checkbox.setchecked(false);                     }                       if (!finalviewholder.checkbox.ischecked()) {                         finalviewholder.checkbox.setchecked(true);                      }                  }             }          }     ); 

i hope can me, thanks.

this because when scroll list setoncheckedchangelistener called.evrytime when getview called...instead of use onclicklistener on checkbox

   checkbox.setonclicklistener(new onclicklistener() {                  @override                 public void onclick(view v) {                      if (checkbox.ischecked()) {                      } else if (!checkbox.ischecked()) {                          }                     }                  }             }); 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -