android - How to SELECT ALL in checkbox -


i have listview , checkbox, how can select checkbox given in listview, below getview() code. there property accomplish task? kindly send suggestions on current code. thanks.

code :

 public class mainactivity extends activity {      mycustomadapter dataadapter = null;     private arraylist<checkbox> checkboxes = new arraylist<>();      private string[] application = { "aa", "bb", "cc"}; // dynamic application names     private string[] device = { "ee", "ff", "gg"}; // dynamic                                                                                 // device                                                                                 // names     private radiogroup radiogroup1;     private radiogroup radiogroup2;     private radiobutton btn;     private radiobutton btn2;     private string text1;     private string text2;     radiobutton button1;     radiobutton button2;     button selectall;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          // generate list view arraylist         displaylistview();          checkbuttonclick();         radiogroup1 = (radiogroup) findviewbyid(r.id.radio1);         radiogroup2 = (radiogroup) findviewbyid(r.id.radio1);          viewgroup hourbuttonlayout = (viewgroup) findviewbyid(r.id.radio1); //                                                                             //                                                                             //                                                                             // id                                                                             // of                                                                             //                                                                             // radiogroup                                                                             //                                                                             // defined         (int = 0; < application.length; i++) {             button1 = new radiobutton(this);             button1.setid(i);             button1.settext(application[i]);             hourbuttonlayout.addview(button1);              radiogroup1.setoncheckedchangelistener(new radiogroup.oncheckedchangelistener() {                    public void oncheckedchanged(radiogroup mradiogroup2, int checkedid2) {                         for(int i=0; i<mradiogroup2.getchildcount(); i++) {                              btn = (radiobutton) mradiogroup2.getchildat(i);                              int t=mradiogroup2.getid();                              system.out.println(t);                               if(btn.getid() == checkedid2) {                                   text1 = btn.gettext().tostring();                                   toast.maketext(getapplicationcontext(), "you selected : " + text1, toast.length_short).show();                                   return;                              }                         }                    }               });          }          viewgroup hourbuttonlayout2 = (viewgroup) findviewbyid(r.id.radio2); //                                                                                 //                                                                                 //                                                                                 // id                                                                                 // of                                                                                 //                                                                                 // radiogroup                                                                                 //                                                                                 // defined         (int = 0; < device.length; i++) {             button2 = new radiobutton(this);             button2.setid(i);             button2.settext(device[i]);             hourbuttonlayout2.addview(button2);              radiogroup2.setoncheckedchangelistener(new radiogroup.oncheckedchangelistener() {                    public void oncheckedchanged(radiogroup mradiogroup, int checkedid) {                         for(int i=0; i<mradiogroup.getchildcount(); i++) {                              btn2 = (radiobutton) mradiogroup.getchildat(i);                              int t=mradiogroup.getid();                              system.out.println(t);                               if(btn2.getid() == checkedid) {                                   text2 = btn2.gettext().tostring();                                   toast.maketext(getapplicationcontext(), "you selected : " + text2, toast.length_short).show();                                   return;                              }                         }                    }               });          }      }      private void displaylistview() {          // array list of countries         arraylist<country> countrylist = new arraylist<country>();         country country = new country("", "al jazeera", false);         countrylist.add(country);         country = new country("", "aa", true);         countrylist.add(country);         country = new country("", "bb", false);         countrylist.add(country);           // create arrayadaptar string array         dataadapter = new mycustomadapter(this, r.layout.country_info,                 countrylist);         listview listview = (listview) findviewbyid(r.id.listview1);         // assign adapter listview         listview.setadapter(dataadapter);          listview.setonitemclicklistener(new onitemclicklistener() {             public void onitemclick(adapterview<?> parent, view view,                     int position, long id) {                 // when clicked, show toast textview text                 country country = (country) parent.getitematposition(position);                 toast.maketext(getapplicationcontext(),                         "clicked on row: " + country.getname(),                         toast.length_long).show();             }         });      }      private class mycustomadapter extends arrayadapter<country> {          private arraylist<country> countrylist;          public mycustomadapter(context context, int textviewresourceid,                 arraylist<country> countrylist) {             super(context, textviewresourceid, countrylist);             this.countrylist = new arraylist<country>();             this.countrylist.addall(countrylist);         }          private class viewholder {             textview code;             checkbox name;         }          @override         public view getview(int position, view convertview, viewgroup parent) {              viewholder holder = null;             log.v("convertview", string.valueof(position));              if (convertview == null) {                 layoutinflater vi = (layoutinflater) getsystemservice(context.layout_inflater_service);                 convertview = vi.inflate(r.layout.country_info, null);                  holder = new viewholder();                 holder.code = (textview) convertview.findviewbyid(r.id.code);                 holder.name = (checkbox) convertview                         .findviewbyid(r.id.checkbox1);                 convertview.settag(holder);                  checkboxes.add(holder.name);                  selectall = (button) findviewbyid(r.id.selectall);                  selectall.setonclicklistener(new onclicklistener() {                      @override                     public void onclick(view arg0) {                          (int = 0; < checkboxes.size(); i++) {                             checkbox currentchecbox = checkboxes.get(i);                              currentchecbox.setchecked(true);                         }                      }                  });                  holder.name.setonclicklistener(new view.onclicklistener() {                     public void onclick(view v) {                         checkbox cb = (checkbox) v;                         country country = (country) cb.gettag();                         toast.maketext(                                 getapplicationcontext(),                                 "clicked on checkbox: " + cb.gettext() + " " + cb.ischecked(), toast.length_long).show();                         country.setselected(cb.ischecked());                     }                 });             } else {                 holder = (viewholder) convertview.gettag();             }              country country = countrylist.get(position);             holder.code.settext("");             holder.name.settext(country.getname());             holder.name.setchecked(country.isselected());             holder.name.settag(country);              return convertview;          }      }      private void checkbuttonclick() {          button mybutton = (button) findviewbyid(r.id.findselected);         mybutton.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                  stringbuffer responsetext = new stringbuffer();                 responsetext.append("the following selected : ");                  arraylist<country> countrylist = dataadapter.countrylist;                 (int = 0; < countrylist.size(); i++) {                     country country = countrylist.get(i);                     if (country.isselected()) {                         responsetext.append(", " + country.getname());                         string response = responsetext.tostring();                     }                 }                  // selected radiobuttons                 if(radiogroup1.getcheckedradiobuttonid()!=-1){                    text1 = btn.gettext().tostring();                    log.d("button", "text 1 : " + text1);                 }                  if(radiogroup2.getcheckedradiobuttonid()!=-1){                    text2 = btn2.gettext().tostring();                    log.d("button", "text 2 : " + text2);                 }                  toast.maketext(getapplicationcontext(), "show names : " + responsetext + "and application : " + text1 + "and device : " + text2, toast.length_long).show();              }         });      }  } 

you create array list holds checkboxes when want them selected loop through array , setchecked(true) on each one. example

declare arraylist @ top of adapter

private arraylist<checkbox> checkboxes = new arrraylist<>(); 

then in getview add checkbox array. make sure add after have instantiated it.

checkboxes.add(holder.name);  

then in onclick

        (int = 0; < checkboxes.size(); i++) {         checkbox currentchecbox = checkboxes.get(i);          currentchecbox.setchecked(true);     } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -