mercredi 2 septembre 2015

Delete Button in Listview from SQLiteDatabase

I was wondering if somebody could further explain how to implement a delete button inside a listview that is populated from an SQLite database. I've read the responses to the following question that is essentially what I'm asking as well, but I don't understand it:

How can I implement a delete button in a ListView and delete from database?

In my custom row .xml file, I included a delete button that implements the method delete() onClick. It also includes an alert dialog, by the way. Here's the code that I have so far for my delete() method; whenever I try to use it, it never gets the right activtiy entry.

  public void delete(View view){

    final int position = listview.getPositionForView((View) view.getParent());
    String id = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_ID));

    AlertDialog.Builder myDialog = new AlertDialog.Builder(MainActivity.this);

    myDialog.setTitle("Delete activity entry \"" + id + "\"?");
    myDialog.setPositiveButton("DELETE", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface arg0, int arg1) {
            mySQLiteAdapter.delete(position);
            cursor.requery();
            }
        });

    myDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface arg0, int arg1) {

            }
        });

    myDialog.show();
}

Aucun commentaire:

Enregistrer un commentaire