Delete data

A user who pulls up a list of notes from the NotesList class can choose the Delete option on the context menu to run the following method: @Override public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo info;

info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo() ;

switch (item.getItemId()) { case MENU_ITEM_DELETE: { // Delete the note that the context menu is for

Uri noteUri = ContentUris.withAppendedId(getIntent().getData(), info.id); getContentResolver().delete(noteUri, null, null); return true;

return false;

Here are some of the highlights of the code:

O When the menu for the job was created, the job ID was stuffed into the extra information variable for the menu. That extra information section is retrieved from the MenuItem on this line and used in the next part of the highlighted code.

© Builds a URI by extracting the URI from the user's Intent, as usual, and appending the number of the item to delete, taken from the menu.

© Creates a ContentResolver and pass the URI to its delete method.

CHAPTER 9

0 0

Post a comment