Android listview getView checkBox -


something strange happening, when click row in list corresponding checkbox selected. right, strange thing that, automatically every 7 rows checkbox setcheked true. what's problem? help

 listadapter adapter = new arrayadapter<dettaglio1>             (this, r.layout.deteails_list_pdf, r.id.tv_nome_categoria, dettagli1) {  @override         public view getview(final int position, view convertview, viewgroup parent) {             view row = super.getview(position, convertview, parent); ... ...  list.setonitemclicklistener(new adapterview.onitemclicklistener() {                 @override                 public void onitemclick(adapterview<?> parent, view view, int position, long id) {                     final dettaglio1 d1 = dettagli1.get(position);                      d1.setchecked(!d1.ischecked());                      checkbox checkbox = (checkbox) view.findviewbyid(r.id.checkbox2);                     checkbox.setchecked(d1.ischecked());                  }             });             return row;         }     };     list.setadapter(adapter); 

private class dettaglio1 {     string nome;      private boolean ischecked;      public void setchecked(boolean ischecked) {         this.ischecked = ischecked;     }      public boolean ischecked() {         return ischecked;     } } 

what's happening known view recycling - core mechanism behind listview , recyclerview. every 7th checkbox checked, because that's same checkbox recycled , displayed previous state.

to fix problem, need keep 'checked' state inside data model (the dettaglio1 class). example, can modify data model so:

public class dettaglio1 {     // stuff     private boolean ischecked;      public void setchecked(boolean ischecked) {         this.ischecked = ischecked;     }      public void ischecked() {         return ischecked;     }     // more stuff } 

and listener so:

@override public void onitemclick(adapterview<?> parent, view view, int position, long id) {     final dettaglio1 d1 = dettagli1.get(position);     d1.setchecked(!d1.ischecked());      checkbox checkbox = (checkbox) view.findviewbyid(r.id.checkbox2);     checkbox.setchecked(d1.ischecked()); } 

edit: need override adapter's getview() method , set checkbox's checked state according dettaglio1 @ current position.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -