json - Get checked items id from custom listview and pass them to new activity android -


i'm developing android app has custom listview checkbox. want pass checked items 1 activity another. how should pass them? , should manage checkbox (to checked items) in custom adapter or activity?

note: retrieve data server using json response.

here's model :

  public class groups {         public string name;         public boolean selected= false;          public string getname() {             return name;         }          public void setname(string name) {             this.name = name;         }          public boolean isselected() {             return selected;         }          public void setselected(boolean selected) {             this.selected = selected;         }          public groups() {         }     } 

my adapter:

public class adaptermainactivity extends baseadapter{

activity activity; private layoutinflater inflater; list<groups> groupslist;  public adaptermainactivity(activity activity, list<groups> groupses) {     this.activity = activity;     this.groupslist = groupses; }  @override public int getcount() {     return groupslist.size(); }  @override public object getitem(int position) {     return groupslist.get(position); }  @override public long getitemid(int position) {     return position; }  @override public view getview(final int position, view convertview, viewgroup parent) {      if (inflater == null) {         inflater = (layoutinflater) activity.getsystemservice(context.layout_inflater_service);     }     if (convertview == null) {         convertview = inflater.inflate(r.layout.custom_list, null);         textview name = (textview) convertview.findviewbyid(r.id.textviewname);         final checkbox checkbox = (checkbox) convertview.findviewbyid(r.id.checkbox);          final groups groups = groupslist.get(position);         name.settext(groupslist.get(position).getname());          checkbox.setchecked(groups.selected);         checkbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {             @override             public void oncheckedchanged(compoundbutton compoundbutton, boolean ischecked) {                 groups.selected = ischecked;                 mainactivity.getinstance().updatearraylist(groupslist);             }         });       }      return convertview; } 

}

mainactivity:

public class mainactivity extends appcompatactivity {      listview listviewgroups;     button buttonsenttoactivity;      list<groups> groupslist;     list<groups> resultgrouplist;     arraylist<boolean> arechecked;     list<string> finalarray;      private adaptermainactivity adaptermainactivity;      static mainactivity youractivity;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          youractivity = this;          groupslist= new arraylist<groups>();         resultgrouplist= new arraylist<groups>();          readgroup(37);          adaptermainactivity = new adaptermainactivity(this, groupslist);          listviewgroups = (listview) findviewbyid(r.id.listviewgroups);         listviewgroups.setadapter(adaptermainactivity);          buttonsenttoactivity = (button) findviewbyid(r.id.buttonsendto2activity);         buttonsenttoactivity.setonclicklistener(buttonsenttoactivitylistener);          log.e("group list size ", string.valueof(groupslist.size()));         finalarray = new arraylist<>();         (int = 0; < resultgrouplist.size(); i++) {             if (resultgrouplist.get(i).selected) {                 finalarray.add(resultgrouplist.get(i).getname());                 log.e("final array size", string.valueof(finalarray.size()));             }         }      }     public void readgroup(long cid) {         response.listener<string> responselistener = new response.listener<string>() {             @override             public void onresponse(string response) {                 try {                     jsonobject jsonobject = new jsonobject(response.tostring());                     jsonarray readarray = jsonobject.getjsonarray("groups");                      (int = 0; < readarray.length(); i++) {                          log.e("i is: ", string.valueof(i));                         jsonobject jssonrow = readarray.getjsonobject(i);                          string groupname = jssonrow.getstring("name");                          groups groups = new groups();                         groups.setname(groupname);                         log.e("name is: ", groupname);                         groupslist.add(groups);                     }                 } catch (jsonexception e) {                     e.printstacktrace();                 }                 adaptermainactivity.notifydatasetchanged();             }         };          log.e("client id is: ", string.valueof(cid));         readgrouprequesr readgrouprequest = new readgrouprequesr(cid, responselistener);         requestqueue queue = volley.newrequestqueue(mainactivity.this);         queue.add(readgrouprequest);          log.e("out of loop", "");     }      public static mainactivity getinstance() {         return youractivity;     }      public void updatearraylist(list<groups> arraylist) {         this.resultgrouplist = arraylist;     }      view.onclicklistener buttonsenttoactivitylistener = new view.onclicklistener() {         @override         public void onclick(view view) {             //bundle b= new bundle();             //b.putstringarraylist("arraylist", (arraylist<string>) finalarray);             intent intent = new intent(mainactivity.this, secondactivity.class);             intent.putstringarraylistextra("arraylist", (arraylist<string>) finalarray);             //intent.putextras(b);             log.e("final array size", string.valueof(finalarray.size()));             startactivity(intent);         }     };  } 

at first, manage checkboxes :

in activity class add boolean array or arraylist having size same list array size , initialize value false :

string[] titlesarray; arraylist<boolean> arrchecked;  // initialize arrchecked boolean array , add checkbox value false each item of listview     arrchecked = new arraylist<boolean>();     (int = 0; < titles.size(); i++) {     arrchecked.add(false);     } 

now replace adapter class :

class vivzadapter extends arrayadapter<string> implements oncheckedchangelistener { context context; int[] images; string[] titlesarray, descrptionarray; list<integer> positions = new arraylist<integer>();  arraylist<boolean> arrchecked;  vivzadapter(context context, string[] titles, int[] images, string[] description, arraylist<boolean> arrchecked) {  super(context, r.layout.single_row, r.id.textview1, titles); this.context = context; this.images = images; this.titlesarray = titles; this.descrptionarray = description; this.arrchecked = arrchecked;  }  class myviewholder { imageview myimage; textview mytitle; textview mydescription; checkbox box;  myviewholder(view v) {     myimage = (imageview) v.findviewbyid(r.id.imageview1);     mytitle = (textview) v.findviewbyid(r.id.textview1);     mydescription = (textview) v.findviewbyid(r.id.textview2);     box = (checkbox) v.findviewbyid(r.id.checkbox1); } }  @override public view getview(int position, view convertview, viewgroup parent) { view row = convertview; myviewholder holder = null; if (row == null) {     // 1.Âștime     layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service);      //row contem relativelayout(root) em single_row.xml     row = inflater.inflate(r.layout.single_row, parent, false);     holder = new myviewholder(row);     row.settag(holder);     //log.d("vivz", "creating new row"); } else {     //reciclamos aqui, qeremos usar antigo objecto holder     holder = (myviewholder) row.gettag();     //log.d("vivz", "recycling stuff"); } holder.myimage.setimageresource(images[position]); holder.mytitle.settext(titlesarray[position]); holder.mydescription.settext(descrptionarray[position]);  //set position id holder.box.setid(position); //set onclicklistener of checkbox rather oncheckedchangelistener holder.box.setonclicklistener(new onclicklistener() {          @override         public void onclick(view v) {             int id = v.getid();              if (arrchecked.get(id)) {                 //if checked, make unchecked                 arrchecked.set(id, false);             } else {                 //if unchecked, make checked                 arrchecked.set(id, true);              }          }     });      //set value of each checkbox arrchecked boolean array     holder.box.setchecked(arrchecked.get(position));  return row; } } 

after that, implement click listener of send button btnsend button (i considering sending data 1 activity activity on click of send button) :

 btnsend.setonclicklistener(new onclicklistener() {          @override         public void onclick(view v) {             arraylist<string> arrtemplist = new arraylist();             for(int i=0; i<titles.size(); i++){                if(arrchecked.get(i) == true){                   arrtemplist.add(titles[i]);                }             }              // here can send arrtemplist having checked items          }     }); 

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) -