android - Handling imageButton clicks inside a custom listview -
i have listview custom adapter. custom row in listview containts 2 imagebuttons need handle when click on it. doesn't work want, because when start app , click on imagebutton nothing happens, if first click on row , on imagebutton, click being handled. tried few solutions helped other users here so, nobody had exact same problem me gets handled after row click.
favoriteactivity
public class favoriteactivity extends activity { private list<favorite> favoriteitem = new arraylist<favorite>(); viewholder holder = new viewholder(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.favorite); populatefavoritelist(); populatelistview(); registerclickcallback(); } private void populatefavoritelist() { favoriteitem.add(new favorite(r.drawable.icon_camera, "item 1")); favoriteitem.add(new favorite(r.drawable.icon_camera, "item 2")); favoriteitem.add(new favorite(r.drawable.icon_camera, "item 3")); favoriteitem.add(new favorite(r.drawable.icon_camera, "item 4")); favoriteitem.add(new favorite(r.drawable.icon_camera, "item 5")); favoriteitem.add(new favorite(r.drawable.icon_camera, "item 6")); favoriteitem.add(new favorite(r.drawable.icon_camera, "item 7")); favoriteitem.add(new favorite(r.drawable.icon_camera, "item 8")); } private void populatelistview() { arrayadapter<favorite> adapter = new mylistadapter(); listview list = (listview) findviewbyid(r.id.favoritelist); list.setadapter(adapter); } private class mylistadapter extends arrayadapter<favorite> { public mylistadapter() { super(favoriteactivity.this, r.layout.favorite_row, favoriteitem); } @override public view getview(int position, view convertview, viewgroup parent) { // sure have view, because null possible view itemview = convertview; if (itemview == null) { itemview = getlayoutinflater().inflate(r.layout.favorite_row, parent, false); } // find item work favorite currentitem = favoriteitem.get(position); // filling view holder.thumbnail = (imagebutton) itemview.findviewbyid(r.id.favoritethumbnail); holder.thumbnail.setbackgroundresource(currentitem.getthumbnail()); holder.name = (textview) itemview.findviewbyid(r.id.favoritename); holder.name.settext(currentitem.getname()); return itemview; } } private void registerclickcallback() { listview list = (listview) findviewbyid(r.id.favoritelist); list.setonitemclicklistener(new adapterview.onitemclicklistener(){ @override public void onitemclick(adapterview<?> parent, view viewclicked, int position, long id) { final favorite favoriteclicked = favoriteitem.get(position); string msg = "clicked position " + position + " " + favoriteclicked.getname();//can use holder here name back?? because wasn't working... toast.maketext(favoriteactivity.this, msg, toast.length_long).show(); imagebutton btndirections = (imagebutton) viewclicked.findviewbyid(r.id.favoritedirections); imagebutton btndelete = (imagebutton) viewclicked.findviewbyid(r.id.favoritedelete); btndirections.setonclicklistener(new onclicklistener() { string btnmsg = "getting directions " + favoriteclicked.getname(); @override public void onclick(view v) { toast.maketext(favoriteactivity.this, btnmsg, toast.length_long).show(); } }); btndelete.setonclicklistener(new onclicklistener() { string btnmsg = "getting delete " + favoriteclicked.getname(); @override public void onclick(view v) { toast.maketext(favoriteactivity.this, btnmsg, toast.length_long).show(); } }); } }); } static class viewholder { imagebutton thumbnail; textview name; } } favoriteclass
public class favorite { private int favoritethumbnail; private string favoritename; public favorite(int favoritethumbnail, string favoritename){ super(); this.favoritethumbnail = favoritethumbnail; this.favoritename = favoritename; } public int getthumbnail(){ return favoritethumbnail; } public string getname(){ return favoritename; } } favorite_row.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightsum="4" android:descendantfocusability="blocksdescendants"> <imagebutton android:layout_width="75dp" android:layout_height="65dp" android:contentdescription="thumbnail" android:src="@drawable/icon_camera" android:layout_weight="2" android:id="@+id/favoritethumbnail" android:scaletype="fitxy" style="@style/favoriterowitems" android:focusable="false"/> <textview android:id="@+id/favoritename" android:layout_weight="2" android:layout_gravity="center_vertical" android:text="name" android:textsize="30sp" style="@style/favoriterowitems"/> <imagebutton android:contentdescription="direction" android:layout_weight="0.5" android:src="@drawable/icon_directions" android:id="@+id/favoritedirections" style="@style/favoriterowitems" android:focusable="false"/> <imagebutton android:contentdescription="delete favorite" android:layout_weight="0.5" android:src="@drawable/icon_delete" android:id="@+id/favoritedelete" style="@style/favoriterowitems" android:layout_marginright="5dp" android:focusable="false"/> </linearlayout>
add 2 buttons getview method, , include them in handler, on pass through getview method want assign click listener this:
holder.btndelete.setonclicklistener(new onclicklistener() { string btnmsg = "getting delete " + favoriteclicked.getname(); @override public void onclick(view v) { toast.maketext(favoriteactivity.this, btnmsg, toast.length_long).show(); } }); so viewholder class this:
static class viewholder { imagebutton thumbnail; textview name; imagebutton btndelete; imagebutton btndirections; } and set buttons in getview() method per other views
holder.btndirections=(imagebutton)itemview.findviewbyid(r.id.favoritedirections); holder.btndelete=(imagebutton)itemview.findviewbyid(r.id.favoritedelete);
Comments
Post a Comment