listview - Set on click listener to a button in a list view Android -
i'm creating item order list restaurrant. goal set onclicklistener each button(+/-) in each row in list can count how many items ordered. can please me code? have managed set listener not count items when clicking among rows.
public class mainactivity extends listactivity { private string[] item_names = {"mozza cheese", "chicken rings", "onion rings", "calamari rings"}; private int item_counter = 1; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.requestwindowfeature(window.feature_no_title); setcontentview(r.layout.view_order_list); listadapter(); } private void listadapter(){ setlistadapter(new myadapter(this, android.r.layout.simple_list_item_2, r.id.items_name_order, item_names)); } private class myadapter extends arrayadapter<string> { public myadapter(context context, int resource, int textviewresourceid, string[] strings) { super(context, resource, textviewresourceid, strings); } @suppresslint("viewholder") public view getview(final int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.view_order, parent, false); textview item_name = (textview) convertview.findviewbyid(r.id.items_name_order); final textview item_count = (textview) convertview.findviewbyid(r.id.count_item_order); button plus = (button) convertview.findviewbyid(r.id.button_plus_order); button minus = (button) convertview.findviewbyid(r.id.button_minus_order); item_name.settext(item_names[position]); plus.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { item_counter++; item_count.settext(integer.tostring(item_counter)); } }); return convertview; } } }
note new int array item_counter
, rest in comment question
public class mainactivity extends listactivity { private string[] item_names = {"mozza cheese", "chicken rings", "onion rings", "calamari rings"}; private int[] item_counter = {0, 0, 0, 0}; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.requestwindowfeature(window.feature_no_title); setcontentview(r.layout.view_order_list); listadapter(); } private void listadapter(){ setlistadapter(new myadapter(this, android.r.layout.simple_list_item_2, r.id.items_name_order, item_names)); } private class myadapter extends arrayadapter<string> { public myadapter(context context, int resource, int textviewresourceid, string[] strings) { super(context, resource, textviewresourceid, strings); } @suppresslint("viewholder") public view getview(final int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.view_order, parent, false); textview item_name = (textview) convertview.findviewbyid(r.id.items_name_order); final textview item_count = (textview) convertview.findviewbyid(r.id.count_item_order); button plus = (button) convertview.findviewbyid(r.id.button_plus_order); button minus = (button) convertview.findviewbyid(r.id.button_minus_order); item_name.settext(item_names[position]); item_count.settext(item_counter[position]; plus.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { item_counter[position]++; notifydatasetchanged(); } }); return convertview; } } }
Comments
Post a Comment