How to make a Button Clickable in a ListView generated using a SimpleAdapter in Android? -
i have generated listview
using simpleadapter
in android. have placed few textviews
, button
in xml
file these widgets displayed on row of listview. want make button clickable in listview. don't want use baseadapter
or customadapter
or arrayadapter
.can suggest me how simpleadapter?
my codes follows:
final listview l=(listview)findviewbyid(r.id.listview1); listadapter k=new simpleadapter(this,val1,r.layout.mytask,new string[]{"taskid","heading","status"},new int[]{r.id.view1,r.id.view2,r.id.viewstatus}); button b=(button)findviewbyid(r.id.mytask); l.setadapter(k);
my xml file follows:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_margintop="18dp" android:text="task name:" android:textsize="15dp" android:textstyle="bold" /> <textview android:id="@+id/view1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:text="textview" android:textsize="15dp" android:textstyle="bold" android:visibility="invisible" /> <textview android:id="@+id/view2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textview1" android:layout_alignbottom="@+id/textview1" android:layout_marginleft="31dp" android:layout_torightof="@+id/textview1" android:text="textview" android:textsize="15dp" android:textstyle="bold" android:visibility="visible" /> <textview android:id="@+id/viewstatus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/view2" android:layout_alignbottom="@+id/view2" android:layout_alignparentright="true" android:text="pending" android:textsize="15dp" android:textstyle="bold" /> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/viewstatus" android:layout_alignbottom="@+id/viewstatus" android:layout_marginright="14dp" android:layout_toleftof="@+id/viewstatus" android:text="status:" android:textsize="15dp" android:textstyle="bold" /> <button android:id="@+id/mytask" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/textview1" android:text="button" />
you can use simpleadapter
yet override getview
. should check docs on how getview
works, how trying accomplish it, seems don't understand adapter basics: button created when listview generates different rows, @ first time, happens asynchronously @ point in future after set adapter.
listadapter k=new simpleadapter(this,val1,r.layout.mytask,new string[]{"taskid","heading","status"},new int[]{r.id.view1,r.id.view2,r.id.viewstatus}) { @override public view getview(int position, view convertview, viewgroup parent) { // filled view simpleadapter view itemview=super.getview(position, convertview, parent); // find our button there view mytaskbutton=itemview.findviewbyid(r.id.mytask); // add onclicklistener mytaskbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // button pressed on view } }); return itemview; } };
or better yet, avoid creating objects inside getview() pre-create listener reuse:
onclicklistener mtaskclicklistener=new onclicklistener() { @override public void onclick(view v) { // button pressed on view, position stored in tag. integer positionpressed=(integer)v.gettag(); } }; listadapter k=new simpleadapter(this,val1,r.layout.mytask,new string[]{"taskid","heading","status"},new int[]{r.id.view1,r.id.view2,r.id.viewstatus}) { @override public view getview(int position, view convertview, viewgroup parent) { // filled view simpleadapter view itemview=super.getview(position, convertview, parent); // find our button there view mytaskbutton=itemview.findviewbyid(r.id.mytask); // add onclicklistener, time alredy created , same mytaskbutton.setonclicklistener(mtaskclicklistener); mytaskbutton.settag(position); // know tag pressed return itemview; } };
Comments
Post a Comment