-1

I came across a number of these which answered the question but didn't exactly structure the classes in a matter I found expandable, a lot of the time it would override functions especially as I was using an SimpleCursorAdapter rather than a BaseAdapter which handles the population of SQLite data into the list item automatically on getView.

The code provided in my answer is provided as a way of showing you how to develop a ListAdapter which will add buttons that can handle click events, WITHOUT enabling click events on the list items or without fully overriding original draw events of the class your extending.

Ricky Khatri
  • 952
  • 2
  • 16
  • 42
Peter Fox
  • 1,809
  • 2
  • 20
  • 34

1 Answers1

0

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.

Ricky Khatri
  • 952
  • 2
  • 16
  • 42
Peter Fox
  • 1,809
  • 2
  • 20
  • 34
  • why to post the question if you know the answer – Ram kiran Pachigolla Dec 26 '12 at 12:48
  • Because a lot of people have asked the question and I found the answer to be complex and doesn't apply to extending more complex classes – Peter Fox Dec 26 '12 at 14:15
  • Hey it work's fine . But i want to use ```textview tv=(TextView)findviewById(R.id.title); tv.settext(Html.fromHtml(myString));``` How ican i do this using Simplecursor adapteter..? – Uday Sep 20 '13 at 06:32