android - Cannot get onClickButton in custom adapter to respond correctly? -
i trying have button's text change on button click in list view, , tried doing through custom adapter. logs onclick called text not seem change:
this adapter code:
public class uservieweradapter extends baseadapter { public list<databaseuser> _list; public list<userresponse> _listforsearch; private final activity mcontext; private confirmmanager confirmmanager; private int query; private button isfriend; // database helper private databasehelper db; private static layoutinflater inflater = null; public uservieweradapter(activity context, list<databaseuser> list) { mcontext = context; _list = list; //establishing db db = new databasehelper(mcontext); confirmmanager = new confirmmanager(); query = 0; } public uservieweradapter( list<userresponse> list, activity context) { mcontext = context; _listforsearch = list; query = 1; //establishing db db = new databasehelper(mcontext); confirmmanager = new confirmmanager(); } @override public int getcount() { if(query == 0) { return _list.size(); } return _listforsearch.size(); } @override public object getitem(int position) { if(query == 0) { return _list.get(position); } return _listforsearch.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(final int position, view convertview, viewgroup parent) { view v = convertview; if (v == null) { layoutinflater inflater = mcontext.getlayoutinflater(); v = inflater.inflate(r.layout.users_layout, null); } imageview userimage = (imageview) v.findviewbyid(r.id.userimage); textview text = (textview) v.findviewbyid(r.id.label); isfriend = (button) v.findviewbyid(r.id.btnfriend); string isfriendtext; //coming search if(query == 1) { if (_listforsearch != null && _listforsearch.size() > 0) { text.settext(_listforsearch.get(position).getname()); } picasso.with(mcontext).setloggingenabled(true); // string picturetoadd = constants.user_images + _listforsearch.get(position).getpicid(); // string photo = _listforsearch.get(position).get_picture(); // log.d(constants.debug, "the photo " + photo); // log.d(constants.debug, "the constant " + constants.user_images); // if (photo.equals("default.jpg")) { picasso.with(mcontext).load(r.drawable.default_profile_image).resize(50, 50).into(userimage); isfriendtext = "add friend"; if(_listforsearch.get(position).isfriend()) { isfriendtext = "remove friend"; } isfriend.settext(isfriendtext); final string finalisfriendtext = isfriendtext; isfriend.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //remove friend callconfirmaddorremove(_listforsearch.get(position), finalisfriendtext); log.d(constants.debug, "clicked friend button " + finalisfriendtext); } }); } return v; } private void changetext(string finalisfriendtext) { string newtexttofill = ""; if(finalisfriendtext.equals("remove friend")) { newtexttofill = "add friend"; } if(finalisfriendtext.equals("add friend")) { newtexttofill = "remove friend"; } log.d(constants.debug, "changing text of friend button " + newtexttofill); isfriend.settext(newtexttofill); notifydatasetchanged(); } private void callconfirmaddorremove(userresponse userresponse, string finalisfriendtext) { boolean dlg = confirmmanager.confirm(mcontext, mcontext.getstring(r.string.remove_friend), mcontext.getstring(r.string.details_confirmation) + "\n" + mcontext.getstring(r.string.details_info), mcontext.getstring(r.string.submit), mcontext.getstring(r.string.cancel), addorremove(userresponse, finalisfriendtext), cancelpost()); } private runnable addorremove(final userresponse userresponse, final string finalisfriendtext) { return new runnable() { public void run() { sendtodb(userresponse, finalisfriendtext); } }; } private void sendtodb(userresponse userresponse, string finalisfriendtext) { log.d(constants.debug, "sending db friend change"); string uid, name, picid; int points, totalshifts, totalhours, isguarding, isfriend; uid = userresponse.getuserid(); name = userresponse.getname(); picid = userresponse.getpictureid(); points = userresponse.gettotalpoints(); totalshifts = userresponse.gettotalshifts(); totalhours = userresponse.gettotalhours(); isguarding = 0; if(userresponse.ischeckedin()) { isguarding = 1; } //have opposite since trying add or remove 3 means newly added friend, 2 means newly removed friend isfriend = 3; if(userresponse.isfriend()) { isfriend = 2; } databaseuser addorremoveuser = new databaseuser(uid, name, picid, points, totalshifts,totalhours, isguarding, isfriend); db.updatefriend(addorremoveuser); changetext(finalisfriendtext); notifydatasetchanged(); } private runnable cancelpost() { return new runnable() { public void run() { log.d(constants.debug, "canceled posting item"); } }; } }
this xml (i set android focusable false listview on itemclick work):
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="5dip" > <!-- listrow left side thumbnail image --> <linearlayout android:id="@+id/thumbnail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="3dip" android:layout_alignparentleft="true" android:layout_marginright="5dip"> <imageview android:id="@+id/userimage" android:layout_width="50dip" android:layout_height="50dip" android:focusable="false" android:focusableintouchmode="false"/> </linearlayout> <!-- name--> <textview android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_torightof="@+id/thumbnail" android:layout_centerinparent="true" android:layout_marginleft="25dip" android:textcolor="#040404" android:typeface="sans" android:textsize="15dip" android:textstyle="bold" android:text="default name" android:focusable="false" android:focusableintouchmode="false"/> <!-- friend button --> <button android:id="@+id/btnfriend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/friends" android:layout_marginright="5dp" android:layout_alignparentright="true" android:layout_centervertical="true" android:textsize="15dip" android:focusable="false" android:focusableintouchmode="false"/> </relativelayout>
in click listener of getview
, method:
callconfirmaddorremove(_listforsearch.get(position), finalisfriendtext);
you not right position
, have define final position
, or binding position
tag button isfriend
.
[update]
isfriend.settag(position); //this inside getview
and in listener:
int position = (int)v.gettag(); //this position correct 1
hope help!
Comments
Post a Comment