listview - Android - Unable to increment or decrement value from list item -
hi using custom listview getting data server , show in listview.. able data , show in listview dont know implement click event of button inside listitem. there 2 buttons increement , decrement qty. clicklistener working not working in right manner. please me correcting issue. did search many postrs in stack overflow unable understand it...
here adapter class
private activity activity; private layoutinflater inflater; private list<feeditem> feeditems; private list<feeditem> filteredfeeditems; imageview plus; imageview minus; int qty = 0; string result; string formatteddate; int id; public feedlistadapter(activity activity, list<feeditem> feeditems) { this.activity = activity; this.feeditems = feeditems; this.filteredfeeditems = feeditems; } @override public int getcount() { return filteredfeeditems.size(); } @override public object getitem(int position) { return filteredfeeditems.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(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.feed_item, null); feeditem item = filteredfeeditems.get(position); id = item.getid(); convertview.settag(integer.valueof(id)); textview date = (textview) convertview.findviewbyid(r.id.name); textview initiator = (textview) convertview .findviewbyid(r.id.timestamp4); textview assignto = (textview) convertview.findviewbyid(r.id.timestamp); final textview location = (textview) convertview.findviewbyid(r.id.timestamp2); //textview category = (textview) convertview.findviewbyid(r.id.category); data.settext(item.getdata(); price .settext(item.getprice()); description.settext(item.getdescription()); plus = (imageview) convertview.findviewbyid(r.id.btnaddtocart1); plus.settag(item.getid()); plus.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { int position = (integer) view.gettag(); //convertview.settag(integer.valueof(id)); feeditem item = filteredfeeditems.get(position); item.quantity++; location.settext(string.valueof(item.quantity)); } }); minus = (imageview) convertview.findviewbyid(r.id.btnaddtocart5); minus.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { integer taggedposition = (integer) view.gettag(); if (qty>0){ //minus.setimageresource(r.drawable.minus_pressed); qty--; location.settext(string.valueof(qty)); } else{ qty = 0; } } }); return convertview; }
this feeditem class:
public class feeditem { int quantity = 0; int id; public feeditem(int id,int quantity){ super(); this.id = id; this.quantity = quantity; } public int getid() { return id; } public void setid(int id) { this.id = id; } public int getquantity() { return quantity; } public void setquantity(int quantity) { this.quantity = quantity; }
currently have 1 variable per activity store quantity. if want store per item, should add: int qty;
member in feeditem
class.
remove:
convertview.settag(integer.valueof(id));
and similar plus, you'll need add:
minus.settag(item.getid());
then instead of qty++;
or in every place in onclick instead of qty
you'll need use item.qty
:
integer id = view.gettag(); feeditem item = filteredfeeditems.get(id); item.qty++; location.settext(string.valueof(item.qty));
update:
i think you're mixing feeditem.id position. if you're not changing order / amount of items in list use position. instead of:
minus.settag(item.getid());
try:
minus.settag(position);
and same plus
Comments
Post a Comment