android - Custom Listener on Button click in ListView Item -
i have created custom listener interface button
click in adapter class, followed tutorial: http://www.c-sharpcorner.com/uploadfile/9e8439/create-custom-listener-on-button-in-listitem-listview-in-a/
adapter:
holder.btnqtyincrease.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (customlistner != null) { customlistner.onbuttonclicklistner(position); } cart = cartarraylist.get(position); holder.textviewquantity.settag(cart); if(cart.getquantity() == 9) { toast.maketext(context, "already reached", toast.length_long).show(); return ; } else { cart.setquantity(cart.getquantity() + 1); holder.textviewquantity.settext(string.valueof(cart.getquantity())); totalprice = cart.getquantity() * cart.getprice(); cartarraylist.cartarraylist.get(position).settotal(totalprice); holder.textviewtotal.settext(cart.gettotal()); } } }); ..... return convertview; }
and have implemented listener
in activity
this:
public class cartactivity extends activity implements custombuttonlistener { ...... @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); log.d("cartactivity-oncreate", "oncreate"); setcontentview(r.layout.activity_cart); ...... } @override public void onresume() { super.onresume(); log.d("cartactivity-onresume", "onresume"); } @override public void onbuttonclicklistner(int position) { totalprice = cartarraylist.cartarraylist.get(position).getquantity() * cartarraylist.cartarraylist.get(position).getprice(); cartarraylist.cartarraylist.get(position).settotal(totalprice); subtotal = subtotal + totalprice; } }
as can see inside for loop setting
, getting total
of each , every list item, , calculating subtotal of arraylist items...
but whenever tap on btnqtyincrease makes change in total of list item
price, not effect on subtotal
amount
you should change logic in way it calculates subtotal in adapter , pass final value interface set in textview
so code should like:
holder.btnqtyincrease.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { cart = cartarraylist.get(position); holder.textviewquantity.settag(cart); if(cart.getquantity() == 9) { toast.maketext(context, "already reached", toast.length_long).show(); return ; } else { cart.setquantity(cart.getquantity() + 1); holder.textviewquantity.settext(string.valueof(cart.getquantity())); totalprice = cart.getquantity() * cart.getprice(); cartarraylist.cartarraylist.get(position).settotal(totalprice); holder.textviewtotal.settext(cart.gettotal()); } } // calculate total here if (customlistner != null) { customlistner.onbuttonclicklistner(gettotalvalue()); } });
define method gettotalvalue
in adapter iterate through array , find subtotal value.
and set in textview
@override public void onbuttonclicklistner(float subtotal ) { txtview.settext(string.valueof(subtotal)); }
accordingly, change interface signature :
public void onbuttonclicklistner(float total);
Comments
Post a Comment