The method below happens when an item in a ListActivity
is long pressed. The idea is to delete that item from the database and for that I need to call mNotesAdapter.deleteNote(ID)
. Which works fine if I don't use an AlertDialog
; but I should use one for delete confirmations. But I don't know how to pass the menu info, or the id itself to the onClick
method.
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo)item.getMenuInfo();
switch (item.getItemId()) {
case R.id.contextmenu_item_remove:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to delete this note?");
builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// VARIABLE menuInfo IS NOT ACCESSIBLE HERE, NOW WHAT?
mNotesAdapter.deleteNote(menuInfo.id);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.show();
return true;
}
return super.onContextItemSelected(item);
}