I'm trying to delete the item from the database by longPress on the listview. When I click delete the item is removed from the listview and shows a Toast message deleted but if I moves to other Activity and returns back the item still exits in my listview. How to remove the item from database
Here, this is my code
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(final AdapterView<?> arg0, View arg1, final int arg2, final long arg3) {
final AlertDialog.Builder delete = new AlertDialog.Builder(AddCount.this);
delete.setIcon(R.drawable.ic_baseline_delete_24);
delete.setTitle("Are you sure");
delete.setMessage("Do you want to delete this item?");
delete.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
DbHandler db = new DbHandler(getApplicationContext());
db.deleteData(arg2 + "");
userList.remove(arg2);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
}
});
delete.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
delete.show();
return true;
}
});
Dbhandler.java(SQLite database)
public void deleteData(String id)
{
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_Inputs, "id = ? ", new String[]{id});
db.close();
}