Firstly your XML for the row, only the ID of the4 button needs to be updated in code if your going to make any changes to it, the rest is not required by the custom part:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:id="@+id/tag_row_layout"
android:orientation="horizontal" android:layout_width="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="fill_parent" android:id="@+id/name"
/>
<Button
android:id="@+id/delete_button"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Do Stuff"
/>
</LinearLayout>
This is a very simple list view and using LinearLayout it'll just display the button next to the text, it's nothing amazing.
The next part then is to have the code for the Adapter like this...
public class CustomListAdapter extends SimpleCursorAdapter {
public CustomListAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = super.getView(position, convertView, parent);
Button btn = (Button)convertView.findViewById(R.id.delete_button);
btn.setOnClickListener(new DeleteButton());
return convertView;
}
protected final static class DeleteButton implements OnClickListener {
public void onClick(View v) {
Log.e("TestButton", "It's been fired!!!");
}
}
}
You can then extend this to multiple buttons but creating more in the list item's XML
and then by creating another static class
in the CustomListAdapter
. This is great for extending the SimpleCursorAdapter
because it will still inject the right data into the correct text views.