title: How to delete row in listview with SimpleCursorAdapter using button
body: <p>I added a delete button in my ListView row and am trying to find a way to make the delete button work inside of my ListView. I can have my code delete the item from the database, I just need help finding where and how to attach a click event to the row's delete button. </p>
  
  <p>I tried creating a custom cursor adapter, but ran into tons of errors when using my application. Any help is greatly appreciated!</p>
  
  <p>Here's my code:</p>
  
  <p>ListRecipients.java</p>
  
  <pre><code>import android.app.Activity;
  import android.content.Context;
  import android.content.Intent;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.graphics.Color;
  import android.os.Bundle;
  import android.support.v4.widget.SimpleCursorAdapter;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemClickListener;
  import android.widget.Button;
  import android.widget.LinearLayout;
  import android.widget.ListView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class ListRecipients extends Activity
  {
      ListView list;
      Cursor cursor;
      SimpleCursorAdapter adapter;
      //CursorAdapter adapter;
  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.list);
  
          DbHelper dbhelper = new DbHelper(getApplicationContext());
          SQLiteDatabase db = dbhelper.getWritableDatabase();
  
          String[] from = new String[] { DbHelper.NAME, DbHelper.PHONE, DbHelper.MESSAGE };
          String[] column = new String[] {DbHelper.ID, DbHelper.NAME, DbHelper.PHONE, DbHelper.MESSAGE };
          int[] toViewIDs = new int[] { R.id.text_list_name, R.id.text_list_phoneNum, R.id.text_list_message };
  
          cursor = db.query(DbHelper.TABLE_NAME, column, null, null, null, null, null);
          adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, toViewIDs, 0);
          //adapter = new CursorAdapter(this, cursor);
  
          list = (ListView)findViewById(R.id.listview_list);
          list.setAdapter(adapter);
          cursor.moveToFirst();
  
          while (cursor.moveToNext())
          {
              String name = cursor.getString(cursor.getColumnIndex(DbHelper.NAME));
              String phone = cursor.getString(cursor.getColumnIndex(DbHelper.PHONE));
              String message = cursor.getString(cursor.getColumnIndex(DbHelper.MESSAGE));
  
              Toast.makeText(getApplicationContext(), "Name = " + name + "\nPhone = " + phone + "\nMessage = " + message, Toast.LENGTH_SHORT).show();
          }
      }
  }
  </code></pre>
  
  <p>CustomCursorAdapter.java</p>
  
  <pre><code>import android.content.Context;
  import android.database.Cursor;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.CursorAdapter;
  import android.widget.Toast;
  
  public class UserCustomAdapter extends CursorAdapter {
      Cursor cursor;
      LayoutInflater inflater;
      Context context;
  
      public UserCustomAdapter(Context context, Cursor cursor) {
          super(context, cursor);
          this.cursor = cursor;
          this.context = context;
          inflater = LayoutInflater.from(context);
      }
  
      @Override
      public void bindView(View view, Context context, final Cursor cursor) {
  
          final String itemId = cursor.getString(cursor.getColumnIndex("ID"));
          Button btnDeleteRecipient = (Button) view.findViewById(R.id.btn_list_delete);
  
          btnDeleteRecipient.setOnClickListener(new OnClickListener() {
              public void onClick(View arg0) {
                  Log.i("ListRecipient", "Delete clicked");
                  //deleteRecordWithId(itemId);
                  cursor.requery();
                  notifyDataSetChanged();
              }
          });
      }
  
      @Override
      public View newView(Context context, Cursor cursor, ViewGroup parent) {
          View v = inflater.inflate(R.layout.list_item, null); 
          return v;
      }
  }
  </code></pre>