android - Holder onClick() taking action on wrong rows -



android - Holder onClick() taking action on wrong rows -

i have custom arrayadapter listview has multiple buttons. when click button in row action wrong row. example; when clicked first row's button, click action working row.

getview codes here:

public view getview(final int position, view view, viewgroup parent) { this.position = position * 2; layoutinflater inflater = context.getactivity().getlayoutinflater(); if (view == null) { view = inflater.inflate(r.layout.item_galery2_list, parent, false); holder = new holder(view); view.settag(holder); } else { holder = (holder) view.gettag(); } ((textview) view.findviewwithtag("textview1")).settext(list .get(this.position).yazi.tostring()); ((textview) view.findviewwithtag("textview2")).settext(list .get(this.position + 1).yazi.tostring()); holder.getimage1().setimagebitmap((list.get(this.position).image)); holder.getimage2().setimagebitmap((list.get(this.position + 1).image)); final holder h1 = holder; holder.btn_follow1().setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (h1.btn_follow1().getbackground().getconstantstate()== context.getresources().getdrawable(r.drawable.followoff).getconstantstate()) { h1.btn_follow1().setbackgroundresource(r.drawable.followon); } else if(h1.btn_follow1().getbackground().getconstantstate()== context.getresources().getdrawable(r.drawable.followon).getconstantstate()) { h1.btn_follow1().setbackgroundresource(r.drawable.followoff); notifydatasetchanged(); } } });

class holder codes here:

public class holder { private view row; private imageview img_img; private imageview img_img2; private button btn_follow1; private button btn_follow2; public holder(view row) { this.row = row; } public imageview getimage1() { if (img_img == null) { img_img = (imageview) row.findviewbyid(r.id.img_img1); } homecoming img_img; } public imageview getimage2() { if (img_img2 == null) { img_img2 = (imageview) row.findviewbyid(r.id.img_img2); } homecoming img_img2; } public button btn_follow1() { if (btn_follow1 == null) { btn_follow1 = (button) row.findviewbyid(r.id.btn_follow1); } homecoming btn_follow1; } public button btn_follow2() { if (btn_follow2 == null) { btn_follow2 = (button) row.findviewbyid(r.id.btn_follow2); } homecoming btn_follow2; } }

whats best way prepare problem?

edit:

when clicked first row's follow button;

before clicked:

and after clicked

its because this.position = position * 2;. hence :

position 0 (first) still 0 position 1 become 2 position 2 become 4 and going on

you might want reconsider how manage position variable. in normal situation (ex : if user click position 0, position 0 activated), dont need position.

android android-arrayadapter android-viewholder

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -