I am trying to delete a single row using onLongClick
of list item but sometime it deletes the row and sometimes doesn't. Maybe I am passing the wrong id to it I don't getting what actually problem is.
I have tried some code and I am trying from last 2 days but unable to resolve this.
Below is my code.
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, final long id) {
AlertDialog.Builder alert = new AlertDialog.Builder(FavouriteListActivity.this);
alert.setMessage("Do you want to unfavorite selected item ?");
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("===========", "Position === " + position);
Log.i("===========", "long id === " + id);
deleteMethod(id);
showFavouriteList();
dialog.cancel();
}
});
alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alert.show();
return true;
}
And deleteMethod
private void deleteMethod(long Id) {
try {
long shId = Id + 1;
SQLiteDatabase db = OpenHelper.getDataHelper(getApplicationContext());
String query = "DELETE FROM " + LocalDatabase.FAVORITE_LIST.TABLE_FAVOURITE_LIST + " WHERE " + LocalDatabase.FAVORITE_LIST.FAVOURITE_LIST_ID + " = " + shId;
Log.i("QUERY", "" + query);
db.execSQL(query);
db.close();
} catch (Exception e) {
e.printStackTrace();
}
}
I am not getting any error, though, but it is not deleting item from database or from listview