title: How to Perform Update and Delete operation in listview item row while Click the button in CursorAdapter
body: <p>With the help of sqlite database I am added the Cricket player name and finally it display the names in listview.</p>
  
  <p>I am added the Update and delete button in CustomCursorAdapter.java.Below I am posted the Adapter code for that:</p>
  
  <p><strong>CustomCursorAdapter.java:</strong></p>
  
  <pre><code>import android.content.Context;
  import android.database.Cursor;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.CursorAdapter;
  import android.widget.TextView;
  
  public class CustomCursorAdapter extends CursorAdapter {
  
      public CustomCursorAdapter(Context context, Cursor c) {
          super(context, c);
      }
  
      @Override
      public View newView(Context context, Cursor cursor, ViewGroup parent) {
          // when the view will be created for first time,
          // we need to tell the adapters, how each item will look
          LayoutInflater inflater = LayoutInflater.from(parent.getContext());
          View retView = inflater.inflate(R.layout.single_row_item, parent, false);
  
          return retView;
      }
  
      @Override
      public void bindView(View view, Context context, Cursor cursor) {
          // here we are setting our data
          // that means, take the data from the cursor and put it in views
  
          TextView textViewPersonName = (TextView) view.findViewById(R.id.tv_person_name);
          textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
  
        //Handle buttons and add onClickListeners
          Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);
          Button updateBtn = (Button)view.findViewById(R.id.update_btn);
  
          deleteBtn.setOnClickListener(new View.OnClickListener(){
              @Override
              public void onClick(View v) { 
  
                  notifyDataSetChanged();
              }
          });  
  
          updateBtn.setOnClickListener(new View.OnClickListener(){
              @Override
              public void onClick(View v) { 
                  //do something
  
                  notifyDataSetChanged();
              }
          });        
      }
  }  
  </code></pre>
  
  <p><strong>single_row_item.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  
      &lt;Button
          android:id="@+id/update_btn"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          android:layout_toLeftOf="@+id/delete_btn"
          android:text="Update" /&gt;
  
      &lt;Button
          android:id="@+id/delete_btn"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_alignParentTop="true"
          android:text="Delete" /&gt;
  
      &lt;TextView
          android:id="@+id/tv_person_name"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignBaseline="@+id/add_btn"
          android:layout_alignBottom="@+id/add_btn"
          android:layout_alignParentLeft="true"
          android:paddingBottom="5dp"
          android:text="Sample Data"
          android:textSize="15sp" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>PersonDatabaseHelper.java:</strong></p>
  
  <pre><code>import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  import android.util.Log;
  
  public class PersonDatabaseHelper {
  
      private static final String TAG = PersonDatabaseHelper.class.getSimpleName();
  
      // database configuration
      // if you want the onUpgrade to run then change the database_version
      private static final int DATABASE_VERSION = 1;
      private static final String DATABASE_NAME = "mydatabase.db";
  
      // table configuration
      private static final String TABLE_NAME = "person_table";         // Table name
      private static final String PERSON_TABLE_COLUMN_ID = "_id";     // a column named "_id" is required for cursor
      private static final String PERSON_TABLE_COLUMN_NAME = "person_name";
      private static final String PERSON_TABLE_COLUMN_PIN = "person_pin";
  
      private DatabaseOpenHelper openHelper;
      private SQLiteDatabase database;
  
      // this is a wrapper class. that means, from outside world, anyone will communicate with PersonDatabaseHelper,
      // but under the hood actually DatabaseOpenHelper class will perform database CRUD operations 
      public PersonDatabaseHelper(Context aContext) {
  
          openHelper = new DatabaseOpenHelper(aContext);
          database = openHelper.getWritableDatabase();
      }
  
      public void insertData (String aPersonName, String aPersonPin) {
  
          // we are using ContentValues to avoid sql format errors
  
          ContentValues contentValues = new ContentValues();
  
          contentValues.put(PERSON_TABLE_COLUMN_NAME, aPersonName);
          contentValues.put(PERSON_TABLE_COLUMN_PIN, aPersonPin);
  
          database.insert(TABLE_NAME, null, contentValues);
      }
  
      public Cursor getAllData () {
  
          String buildSQL = "SELECT * FROM " + TABLE_NAME;
  
          Log.d(TAG, "getAllData SQL: " + buildSQL);
  
          return database.rawQuery(buildSQL, null);
      }
  
      // this DatabaseOpenHelper class will actually be used to perform database related operation 
  
      private class DatabaseOpenHelper extends SQLiteOpenHelper {
  
          public DatabaseOpenHelper(Context aContext) {
              super(aContext, DATABASE_NAME, null, DATABASE_VERSION);
          }
  
          @Override
          public void onCreate(SQLiteDatabase sqLiteDatabase) {
              // Create your tables here
  
              String buildSQL = "CREATE TABLE " + TABLE_NAME + "( " + PERSON_TABLE_COLUMN_ID + " INTEGER PRIMARY KEY, " +
                      PERSON_TABLE_COLUMN_NAME + " TEXT, " + PERSON_TABLE_COLUMN_PIN + " TEXT )";
  
              Log.d(TAG, "onCreate SQL: " + buildSQL);
  
              sqLiteDatabase.execSQL(buildSQL);
          }
  
          @Override
          public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
              // Database schema upgrade code goes here
  
              String buildSQL = "DROP TABLE IF EXISTS " + TABLE_NAME;
  
              Log.d(TAG, "onUpgrade SQL: " + buildSQL);
  
              sqLiteDatabase.execSQL(buildSQL);       // drop previous table
  
              onCreate(sqLiteDatabase);               // create the table from the beginning
          }
      }
  }
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/Y2Apq.png" alt="enter image description here"></p>
  
  <p>In that above screenshot,it shows the Update and delete button.If I click the Update button it have to show an alert dialog and edit the name of the specified listview and display it.</p>
  
  <p>Then if I click the Delete button,it have to delete the appropriate listview row.</p>
  
  <p>Anybody can help me with this.Thank you.</p>