android - Recyclerview with footer not able to delete last item -
i using recyclerview footer working fine. not able delete last item. suppose have 2 products in list if user remove 1 product 1 product remain in recyclerview
not able delete remaining product.
while have 1 item shows pack id null
genericviewholder.removes.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { packid=currentitem.getcart_product_packid(); system.out.println("pack id"+packid); deletetocart(); } });
adapter
public class headerfooteradapter extends recyclerview.adapter<recyclerview.viewholder> { private static final int type_header = 0; private static final int type_item = 1; private static final int type_footer = 2; private arraylist<cartmodel> idlistdata; private aquery aquery; context context; public headerfooteradapter(context context,arraylist<cartmodel> idlistdata) { this.context = context; this.idlistdata = idlistdata; aquery = new aquery(this.context); } @override public recyclerview.viewholder oncreateviewholder (viewgroup parent, int viewtype) { if(viewtype == type_header) { view v = layoutinflater.from (parent.getcontext ()).inflate (r.layout.item_header, parent, false); return new headerviewholder (v); } else if(viewtype == type_footer) { view v = layoutinflater.from (parent.getcontext ()).inflate (r.layout.item_footer, parent, false); return new footerviewholder (v); } else if(viewtype == type_item) { view v = layoutinflater.from (parent.getcontext ()).inflate (r.layout.listitem_buynow, parent, false); return new genericviewholder (v); } return null; } private cartmodel getitem (int position) { return idlistdata.get (position); } @override public void onbindviewholder (recyclerview.viewholder holder, int position) { if(holder instanceof headerviewholder) { headerviewholder headerholder = (headerviewholder) holder; headerholder.txttitleheader.settext ("header"); headerholder.txttitleheader.setonclicklistener (new view.onclicklistener () { @override public void onclick (view view) { // toast.maketext (context, "clicked header", toast.length_short).show (); } }); } else if(holder instanceof footerviewholder) { footerviewholder footerholder = (footerviewholder) holder; footerholder.txttitlefooter.settext ("footer"); footerholder.footercheckout.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { // toast.maketext(context, "clicked footer", toast.length_short).show(); if(userd > 0) { system.out.println("positive"); intent intent=new intent(buynowactivity.this,delieveringproduct.class); startactivity(intent); } else { system.out.println("negative"); string skipsid=string.valueof(userd); intent intent=new intent(buynowactivity.this,login_page.class); intent.putextra("proceedtocheckoutid",skipsid); intent.putextra("ischeckout",true); startactivity(intent); } } }); } else if(holder instanceof genericviewholder) { final cartmodel currentitem = getitem (position - 1); final genericviewholder genericviewholder = (genericviewholder) holder; genericviewholder.txtname.settext(currentitem.getcart_product_name()); string pics = currentitem.getcart_product_img(); aquery.id(genericviewholder.proimage).image(pics, true, true, 0, r.mipmap.ic_launcher); genericviewholder.buynowprice.settext("$" + currentitem.getcart_product_price()); genericviewholder.buynowqtys.settext(currentitem.getcart_product_qty()); genericviewholder.discounttext.settext(currentitem.getcart_product_packdiscount()); subtotal.settext("$" + currentitem.getcart_product_price()); genericviewholder.edtcoupan.settext(currentitem.getcart_product_coupancode()); genericviewholder.gocoupan.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if(genericviewholder.edtcoupan.gettext().tostring().trim().length() > 0) { couponcode=genericviewholder.edtcoupan.gettext().tostring(); packid=currentitem.getcart_product_packid(); system.out.println("coupan code on click"+couponcode); goforcoupan(); } } }); genericviewholder.removes.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { packid=currentitem.getcart_product_packid(); system.out.println("pack id"+packid); deletetocart(); } }); } @override public int getitemviewtype (int position) { if (ispositionheader (position)) { return type_header; } else if(ispositionfooter (position)) { return type_footer; } return type_item; } private boolean ispositionheader (int position) { return position == 0; } private boolean ispositionfooter (int position) { return position == idlistdata.size () + 1; } @override public int getitemcount () { return idlistdata.size () + 2; } class footerviewholder extends recyclerview.viewholder { button footercheckout; textview txttitlefooter; public footerviewholder (view itemview) { super (itemview); this.txttitlefooter = (textview) itemview.findviewbyid (r.id.txtfooter); this.footercheckout = (button) itemview.findviewbyid (r.id.footercheckout); } } class headerviewholder extends recyclerview.viewholder { textview txttitleheader; public headerviewholder (view itemview) { super (itemview); this.txttitleheader = (textview) itemview.findviewbyid (r.id.txtheader); } } class genericviewholder extends recyclerview.viewholder { textview discounttext; linearlayout lindiscount; edittext edtcoupan; textview txtname,buynowprice,buynowqtys,buynowfreq,gocoupan,removes,firsttxt,thirdtxt; public imageview proimage; public relativelayout linfreq,linqty; public genericviewholder (view itemview) { super (itemview); this.txtname = (textview) itemview.findviewbyid (r.id.listitem_buynow_title); this.proimage = (imageview)itemview.findviewbyid(r.id.listitem_buynow_image); this.buynowprice = (textview)itemview.findviewbyid(r.id.listitem_buynow_price); this.buynowqtys = (textview)itemview.findviewbyid(r.id.listitem_buynow_qtys); this.buynowfreq = (textview)itemview.findviewbyid(r.id.listitem_buynow_freq); this.gocoupan = (textview)itemview.findviewbyid(r.id.listitem_buynow_gocoupan); this.edtcoupan = (edittext)itemview.findviewbyid(r.id.listitem_buynow_coupan); this.removes = (textview)itemview.findviewbyid(r.id.listitem_buynow_remove); this.linfreq = (relativelayout)itemview.findviewbyid(r.id.linfreq); this.linqty = (relativelayout)itemview.findviewbyid(r.id.linqty); this.lindiscount = (linearlayout)itemview.findviewbyid(r.id.coupancodeavailable); this.discounttext = (textview)itemview.findviewbyid(r.id.listitem_buynow_discountstring); this.firsttxt = (textview)itemview.findviewbyid(r.id.firsttext); this.thirdtxt = (textview)itemview.findviewbyid(r.id.thirdtext); } } }
deletetocart
jsonarrayrequest request = new jsonarrayrequest(request.method.post, addtocarturl, new jsonobject(params), new response.listener<jsonarray>() { @override public void onresponse(jsonarray response) { system.out.println("response -->> " + response.tostring()); cartlist=new arraylist<cartmodel>(); try { (int = 0; < response.length(); i++) { jsonobject person = (jsonobject) response .get(i); system.out.println("person"+person); string responsecode = person.getstring("subtotal"); system.out.println("subtot"+responsecode); system.out.println("person"+person); if(person.getstring("responsecode").equals("1")) { jsonarray itemslist=person.optjsonarray("itemlist"); if(itemslist==null) { toast.maketext(buynowactivity.this, "your shopping cart empty", toast.length_short).show(); } else { (int j = 0; j < itemslist.length(); j++) { jsonobject cartitems = itemslist.getjsonobject(j); cartmodel cvm = new cartmodel(); cvm.setcart_product_name(cartitems.getstring("prodnarration")); cvm.setcart_product_price(cartitems.getstring("finalprice")); cvm.setcart_product_id(cartitems.getstring("productid")); cvm.setcart_product_img(cartitems.getstring("packlink")); cvm.setcart_product_qty(cartitems.getstring("qty")); cvm.setcart_product_packdiscount(cartitems.getstring("packdiscdesc")); cartlist.add(cvm); } toast.maketext(buynowactivity.this, "product removed successfully", toast.length_short).show(); } } else { system.out.println("something wrong"); } rcadapter = new headerfooteradapter(buynowactivity.this,cartlist); rview.setadapter(rcadapter); }
this guess not parsing packid
while re initializing list after deleting 1 item.
if(person.getstring("responsecode").equals("1")) { jsonarray itemslist=person.optjsonarray("itemlist"); if(itemslist==null) { toast.maketext(buynowactivity.this, "your shopping cart empty", toast.length_short).show(); } else { (int j = 0; j < itemslist.length(); j++) { jsonobject cartitems = itemslist.getjsonobject(j); cartmodel cvm = new cartmodel(); cvm.setcart_product_name(cartitems.getstring("prodnarration")); cvm.setcart_product_price(cartitems.getstring("finalprice")); cvm.setcart_product_id(cartitems.getstring("productid")); cvm.setcart_product_img(cartitems.getstring("packlink")); cvm.setcart_product_qty(cartitems.getstring("qty")); cvm.setcart_product_packdiscount(cartitems.getstring("packdiscdesc")); // here add code parsing packid cartlist.add(cvm); } toast.maketext(buynowactivity.this, "product removed successfully", toast.length_short).show(); } }
try adding parsing code have added comment.
hope you.
Comments
Post a Comment