android - How to add Button action in list view in simple cursor adapter -
i have list view shows names fetching database using simple cursor adapter this-
wordsdb ob=new wordsdb(this.getactivity(),"dba",null,1); simplecursoradapter adapter=new simplecursoradapter(this.getactivity(),r.layout.list_row,c,new string[]{ob.val},new int[]{r.id.textview1},0); listview lv=(listview)v.findviewbyid(r.id.list); lv.setadapter(adapter);
now, want have buttons edit , delete in front of each contact i.e in each row. added buttons in layout file list_row.xml how add listeners clicks on "edit" , "delete" buttons.
tried searching alot not find suitable solution. tried using custom cursor adapter ended confusing should add data list view i.e. should use above code in newview() or bindview() method.
this new list_row.xml file buttons.
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="textview" /> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" /> <button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" /> </linearlayout>
kindly guide me .
you have create custom adapter class extending simplecursoradapter , in bindview() method, can implement button listeners.
something this,
public class mysimplecursoradapter extends simplecursoradapter { public mysimplecursoradapter(context context, int layout, cursor c, string[] from, int[] to, int flags) { super(context, layout, c, from, to, flags); } @override public view getview(int position, view convertview, viewgroup parent) { if (convertview != null) { return convertview; } return layoutinflater.from(context).inflate(r.layout.listform_item); } @override public void bindview(view view, context context, cursor cursor) { button yourbutton = (button) findviewbyid(r.id.ibtndelete); yourbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { } }); } }
edited
to make work cursoradapter,
public class customcursoradapter extends cursoradapter{ public customcursoradapter(context context, cursor c, int flags){ super(context,c,flags); } @override public void bindview(view view, context context, cursor cursor){ button yourbutton = (button) findviewbyid(r.id.ibtndelete); yourbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { } }); } @override public view newview(context context, cursor cursor, viewgroup parent){ // stuff } }
Comments
Post a Comment