android - Add checked CheckBox in Array, remove unchecked one from Array -


i can check several checkboxes can uncheck them before clicking button.
have table in insert clicked checkbox.
problem 1 uncheck inserted.
code:

public class inscriptionassuranceremorqueur extends appcompatactivity implements adapterview.onitemclicklistener, view.onclicklistener {  public static arraylist<assurance> assurancearray = new arraylist<assurance>(); private listview list_assurance; private button btn_confirmation; private checkbox checkbox;  private double largeur; private double longueur; private double poids; private string nom; private string mail; private string tel; private string mdp; private textview txt; arraylist<assurance> tab = new arraylist<assurance>(); private checkbox chkios;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.fen_inscription_assurance_remorqueur);      //intent     intent x = this.getintent();     nom = x.getextras().getstring("nomcompagnie");     mail = x.getextras().getstring("mail");     tel = x.getextras().getstring("tel");     mdp = x.getextras().getstring("mdp");     string largeur = x.getextras().getstring("largeur");     string longueur = x.getextras().getstring("longueur");     string poids  = x.getextras().getstring("poids");     this.largeur = double.parsedouble(largeur);     this.longueur = double.parsedouble(longueur);     this.poids = double.parsedouble(poids);       //remplir le tab des compagnies d'assurance     assurancearray.add(new assurance("gat@gmail.com","gat"));     assurancearray.add(new assurance("star@gmail.com","star"));     assurancearray.add(new assurance("comar@gmail.com","comar"));     assurancearray.add(new assurance("ctama@gmail.com","ctama"));      //récupération      list_assurance = (listview)findviewbyid(r.id.list_assurance);     btn_confirmation = (button)findviewbyid(r.id.btn_cfrm_as_rm);      //adapter     monadapter adapter = new monadapter(this,assurancearray);     list_assurance.setadapter(adapter);     list_assurance.setonitemclicklistener(this);      arraylist<string> selectedstrings = new arraylist<string>();      //ecouteurs      btn_confirmation.setonclicklistener(this);   }  @override public void onitemclick(adapterview<?> parent, view view, int position, long id) {       // when clicked, show toast textview text     assurance = (assurance) parent.getitematposition(position);     toast.maketext(getapplicationcontext(),             "clicked on row: " + a.getnomcompagnie(),             toast.length_long).show();   }  @override public void onclick(view v) {     int i=0,j=0;     boolean test=true;      if(v==btn_confirmation)     {           while (j<assurancearray.size())         {             log.d("test",assurancearray.get(j).getnomcompagnie()+" "+j);             j++;          }     }}    public void itemclicked(view v) {     //code check if checkbox checked!     int i=0;        checkbox checkbox = (checkbox)v;      assurancearray.add(new assurance ("email",checkbox.gettext().tostring()));     }    } 

how do?

you have 2 problems.

  • you never check checkbox state (checked or not)
  • you add element assurancearray on view click. should add if checkbox got checked. remove element array if getting unchecked.

so have :

if (checkbox.ischecked()) {      assurancearray.add(new assurance("email",checkbox.gettext().tostring()));   } else {      (assurance assurance : assurancearray) {          if (assurance.getemail().equals(checkbox.gettext().tostring())  {             assurancearray.remove(assurance);           //you can exit loop find reference           break;        }      } } 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -