Skip to content

Commit

Permalink
1.27 Delete pet from editor menu item
Browse files Browse the repository at this point in the history
  • Loading branch information
Beginning Android committed Aug 23, 2016
1 parent d90609a commit c1a4418
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
59 changes: 58 additions & 1 deletion app/src/main/java/com/example/android/pets/EditorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.

Copy link
@Wolf2231

Wolf2231 Aug 22, 2017

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.

This comment has been minimized.

Copy link
@ShreevaraPunacha

ShreevaraPunacha Aug 22, 2017

May be in any case if dialog is not present, it will be null. so using if statement prevents error due to dismiss for dialog which is no more.

This comment has been minimized.

Copy link
@Wolf2231

Wolf2231 Aug 22, 2017

Well if there is no dialog, there is no button and if there is no button, therefore no need for that step.
The dialog can't be null if it appeared with the delete button in it.

This comment has been minimized.

Copy link
@xht418

xht418 Nov 18, 2018

By the way, I think the "dialog.dismiss()" is also unneeded, unless you override the "dialog.onShowListener()". The default behavior of the dialog is that when you click on positive/negative button it will close the dialog window automatically.

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.

Copy link
@jsouza678

jsouza678 Mar 3, 2020

finish();

will help the app to stay more responsible.
it will come back to the CatalogActivity after the deletion.

}
}

// Close the activity

This comment has been minimized.

Copy link
@seupedro

seupedro Mar 5, 2018

Before you close the activity, you must notify contentResolver() that changes you've made. Simply put this above finish() method.

            getContentResolver().notifyChange(mCurrentPetUri, null);
            finish();

And now your list on CatalogActivity should update!

This comment has been minimized.

Copy link
@KarlRong

KarlRong Apr 14, 2018

This is not necessary. We have already done this in the PetProvider

finish();
}
}
15 changes: 15 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@
<!-- Dialog button text for the option to keep editing the current pet [CHAR LIMIT=20] -->
<string name="keep_editing">Keep Editing</string>

<!-- Toast message in editor when current pet was successfully deleted [CHAR LIMIT=NONE] -->
<string name="editor_delete_pet_successful">Pet deleted</string>

<!-- Toast message in editor when current pet has failed to be deleted [CHAR LIMIT=NONE] -->
<string name="editor_delete_pet_failed">Error with deleting pet</string>

<!-- Dialog message to ask the user to confirm deleting the current pet [CHAR LIMIT=NONE] -->
<string name="delete_dialog_msg">Delete this pet?</string>

<!-- Dialog button text for the option to confirm deleting the current pet [CHAR LIMIT=20] -->
<string name="delete">Delete</string>

<!-- Dialog button text for the option to cancel deletion of the current pet [CHAR LIMIT=20] -->
<string name="cancel">Cancel</string>

<!-- Label for overview category of attributes in the editor [CHAR LIMIT=30] -->
<string name="category_overview">Overview</string>

Expand Down

7 comments on commit c1a4418

@AcidTEX
Copy link

@AcidTEX AcidTEX commented on c1a4418 Mar 4, 2018

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.

@seupedro
Copy link

@seupedro seupedro commented on c1a4418 Mar 5, 2018

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();

@elbhwashy
Copy link

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

@Babadzhanov
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is working perfectly

@jerrychong25
Copy link

@jerrychong25 jerrychong25 commented on c1a4418 Nov 18, 2018

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.

@xht418
Copy link

@xht418 xht418 commented on c1a4418 Nov 18, 2018

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.

@KyawYe-Htut
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting error "Index out of bound exception "
Capture_2020_09_17_16_27_12_341
Capture_2020_09_17_16_27_12_341

Please sign in to comment.