I have a list view that has a list of a user's data. When I long click on the listview item, I want the item to delete. I have it already so that the item looks like it deletes, but when I go back and refresh the page or reopen the page later, the data is still there. I know that's because the item in the database isn't deleted - only the listview one is. I don't know how to delete the database object as well, I've tried a couple of different ways by looking at previous threads but they don't seem to work.
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, final long id) {
AlertDialog.Builder adb = new AlertDialog.Builder(PreviousPractices.this);
adb.setTitle("Delete?");
adb.setMessage("Delete this?");
final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Yes", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
db.remove(id);
ArrayOfPractices.remove(positionToRemove);
adapter.notifyDataSetChanged();
}
});
adb.show();
return false;
}
});
This is the first way I've tried.My remove method in the database class is:
public void remove(long id){
SQLiteDatabase db = this.getWritableDatabase();
String string =String.valueOf(id);
db.delete(DATABASE_TABLE, KEY_ID + " = " + id, null);
}
A second way I've tried is by using this method:
public void deletePractice(Practice practice) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(DATABASE_TABLE, KEY_ID + " = ?",
new String[]{String.valueOf(practice.getID())});
db.close();
}
and then calling it by:
public void onClick(DialogInterface dialog, int which) {
db.deletePractice(pList.get(position));
ArrayOfPractices.remove(positionToRemove);
adapter.notifyDataSetChanged();
}
Aucun commentaire:
Enregistrer un commentaire