-
Notifications
You must be signed in to change notification settings - Fork 717
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -276,7 +276,8 @@ public boolean onOptionsItemSelected(MenuItem item) { | |
return true; | ||
// Respond to a click on the "Delete" menu option | ||
case R.id.action_delete: | ||
// Do nothing for now | ||
// Pop up confirmation dialog for deletion | ||
showDeleteConfirmationDialog(); | ||
return true; | ||
// Respond to a click on the "Up" arrow button in the app bar | ||
case android.R.id.home: | ||
|
@@ -433,4 +434,60 @@ public void onClick(DialogInterface dialog, int id) { | |
AlertDialog alertDialog = builder.create(); | ||
alertDialog.show(); | ||
} | ||
|
||
/** | ||
* Prompt the user to confirm that they want to delete this pet. | ||
*/ | ||
private void showDeleteConfirmationDialog() { | ||
// Create an AlertDialog.Builder and set the message, and click listeners | ||
// for the postivie and negative buttons on the dialog. | ||
AlertDialog.Builder builder = new AlertDialog.Builder(this); | ||
builder.setMessage(R.string.delete_dialog_msg); | ||
builder.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() { | ||
public void onClick(DialogInterface dialog, int id) { | ||
// User clicked the "Delete" button, so delete the pet. | ||
deletePet(); | ||
} | ||
}); | ||
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { | ||
public void onClick(DialogInterface dialog, int id) { | ||
// User clicked the "Cancel" button, so dismiss the dialog | ||
// and continue editing the pet. | ||
if (dialog != null) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ShreevaraPunacha
|
||
dialog.dismiss(); | ||
} | ||
} | ||
}); | ||
|
||
// Create and show the AlertDialog | ||
AlertDialog alertDialog = builder.create(); | ||
alertDialog.show(); | ||
} | ||
|
||
/** | ||
* Perform the deletion of the pet in the database. | ||
*/ | ||
private void deletePet() { | ||
// Only perform the delete if this is an existing pet. | ||
if (mCurrentPetUri != null) { | ||
// Call the ContentResolver to delete the pet at the given content URI. | ||
// Pass in null for the selection and selection args because the mCurrentPetUri | ||
// content URI already identifies the pet that we want. | ||
int rowsDeleted = getContentResolver().delete(mCurrentPetUri, null, null); | ||
|
||
// Show a toast message depending on whether or not the delete was successful. | ||
if (rowsDeleted == 0) { | ||
// If no rows were deleted, then there was an error with the delete. | ||
Toast.makeText(this, getString(R.string.editor_delete_pet_failed), | ||
Toast.LENGTH_SHORT).show(); | ||
} else { | ||
// Otherwise, the delete was successful and we can display a toast. | ||
Toast.makeText(this, getString(R.string.editor_delete_pet_successful), | ||
Toast.LENGTH_SHORT).show(); | ||
This comment has been minimized.
Sorry, something went wrong.
jsouza678
|
||
} | ||
} | ||
|
||
// Close the activity | ||
This comment has been minimized.
Sorry, something went wrong.
seupedro
|
||
finish(); | ||
} | ||
} |
7 comments
on commit c1a4418
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing happens when I confirm the deletion. Pet is still in the list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AcidTEX it happens the same with me, but this helps me. What you should do is notify the contentResolver that changes happened. Try this:
getContentResolver().notifyChange(mCurrentPetUri, null);
finish();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi
i want to to delete a pet from the list item by setOnitemlonglesiner
what should i do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code is working perfectly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Take note that this code only working on Editor Activity, not Catalog Activity.
It is working as of 18th November 2018.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Take note that this code only working on Editor Activity, not Catalog Activity.
It is working as of 18th November 2018.
Same here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why confirming if the dialog is not null? How can it be null?
I think we would just need to call the method dismiss(); on the dialog with out checking if it was null or not, unless someone can prove me wrong.