i have listview in there button in each listview item.whenever user clicks button in each row,i want click count of button in each row seperatelywhen have counter increments user clicks button.but count not seperate each row.it increments when button clicked irrespective of row.
apparently there add button in each listview row.when button clicked,i want change each button label click count.
code:
list = getlistview(); list.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, final int position, long id) { // todo auto-generated method stub textview add = (textview) view .findviewbyid(r.id.btn_add); dishcount = (textview) view .findviewbyid(r.id.textdishcount); ((textview) view .findviewbyid(r.id.btn_add)).setonclicklistener(new onclicklistener() { @override public void onclick(view v) { try { counter.add(position,counter.get(position)+1); } catch (exception e) { e.printstacktrace(); counter.add(position, 1); } totalcount= counter.get(position); toast.maketext(getapplicationcontext(),"value "+totalcount, toast.length_short).show(); dishcount.setvisibility(view.visible); dishcount.settext(string.valueof(totalcount)); } }); } });
update method:
private void updateview(int index){ view v = list.getchildat(index - list.getfirstvisibleposition()); if(v == null) return; count++; textview sometext = (textview) v.findviewbyid(r.id.textdishcount); sometext.setvisibility(view.visible); sometext.settext(string.valueof(count)); }
adapter class:
public class restaurantsubmenulistadapter extends arrayadapter<string> { private final activity context; private final arraylist<bitmap> dishimagesrounded; private final string[] dishname; private final string[] dishdescription; private final string[] dishprice; integer count; textview itemcount; typeface tf,tfb; public restaurantsubmenulistadapter(activity context,arraylist<bitmap> dishimagesbitmaparray, string[] dishnamearray, string[] dishdescriptionarray, string[] dishpricearray, typeface tf, typeface tfb) { super(context, r.layout.restaurant_menu_list_item,dishnamearray); this.context=context; this.dishimagesrounded=dishimagesbitmaparray; this.dishname=dishnamearray; this.dishdescription=dishdescriptionarray; this.dishprice=dishpricearray; this.tf= tf; this.tfb=tfb; } @override public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); view v = convertview; final viewholder h; if(v == null){ v=inflater.inflate(r.layout.restaurant_menu_list_item, parent,false); h = new viewholder(); h.dishname = (textview)v.findviewbyid(r.id.textdishname); h.dishdescription = (textview)v.findviewbyid(r.id.textdishdescription); h.dishprice = (textview)v.findviewbyid(r.id.textdishprice); h.addbutton = (textview)v.findviewbyid(r.id.btn_add); h.imagedish = (imageview) v.findviewbyid(r.id.imagedish); h.dishcount = (textview) v.findviewbyid(r.id.textdishcount); h.addbutton = (textview) v.findviewbyid(r.id.btn_add); v.settag(h); }else { h = (viewholder) v.gettag(); } h.dishname.settypeface(tfb); h.dishdescription.settypeface(tf); h.dishprice.settypeface(tfb); h.addbutton.settypeface(tf); h.dishcount.settypeface(tf); h.dishname.settext(dishname[position]); h.dishdescription.settext(dishdescription[position]); h.dishprice.settext("$"+dishprice[position]); h.imagedish.setimagebitmap(this.dishimagesrounded.get(position)); h.imagedish.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { intent myintent = new intent(context, activityselectingredients.class); myintent.putextra("tablenumber",23); context.startactivity(myintent); } }); /*h.addbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { string tag = (string)v.gettag(); toast.maketext(context, tag, toast.length_short).show(); h.dishcount.setvisibility(view.visible); notifydatasetchanged(); //count++; } });*/ return v; } /** * stop recycling of listview items */ @override public int getviewtypecount() { return getcount(); } /** * stop recycling of listview items */ @override public int getitemviewtype(int position) { return position; } private static class viewholder{ textview dishname,dishdescription,dishprice,addbutton,dishcount; imageview imagedish; }
you need make list or array , save counters. can't have 1 counter rows