Skip to content

Commit

Permalink
1.28 Delete all pets from catalog menu item
Browse files Browse the repository at this point in the history
  • Loading branch information
Beginning Android committed Aug 23, 2016
1 parent c1a4418 commit 311f80d
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion app/src/main/java/com/example/android/pets/CatalogActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand Down Expand Up @@ -118,6 +119,14 @@ private void insertPet() {
Uri newUri = getContentResolver().insert(PetEntry.CONTENT_URI, values);
}

/**
* Helper method to delete all pets in the database.
*/
private void deleteAllPets() {
int rowsDeleted = getContentResolver().delete(PetEntry.CONTENT_URI, null, null);
Log.v("CatalogActivity", rowsDeleted + " rows deleted from pet database");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu options from the res/menu/menu_catalog.xml file.
Expand All @@ -136,7 +145,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
// Respond to a click on the "Delete all entries" menu option
case R.id.action_delete_all_entries:
// Do nothing for now
deleteAllPets();
return true;
}
return super.onOptionsItemSelected(item);
Expand Down

11 comments on commit 311f80d

@ShreevaraPunacha
Copy link

Choose a reason for hiding this comment

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

All the Pet Items are get deleted. but list items on the catalog activity have items. Empty View is appeared only after restarting the app again.. may I know the solution.. tried recreate and finish...no use

@QinwenL
Copy link

Choose a reason for hiding this comment

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

It didn't change after click the button, it is seems the loader hadn't notice the change

@Thejas-Jain
Copy link

Choose a reason for hiding this comment

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

In CatalogActivity, do something like this
@OverRide
public void onLoadFinished(Loader loader, Cursor data) {
if(data != null && data.moveToFirst()) {
mPetCursorAdapter.swapCursor(data);
} else if(data != null && data.getCount() == 0) {
mPetCursorAdapter.swapCursor(null);
}
}

@Babadzhanov
Copy link

Choose a reason for hiding this comment

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

For me everything works perfectly.. it returns to the welcome screen as it should ! 👍
It does make much more sense to ask are you sure you want to delete the WHOLE table instead of a single pet but anyway ;D

@susanschen
Copy link

Choose a reason for hiding this comment

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

The code to refresh the screen are on lines 154 to 182 (click on the expand icon to see hidden lines).

@maginom, to change the text in CatalogActivity to display a different message, add a new string in xml, and then set the message to the new string.

<string name="delete_all_pet_dialog_msg">Delete all pets?</string>
builder.setMessage(R.string.delete_all_pet_dialog_msg);

@biddlecom
Copy link

@biddlecom biddlecom commented on 311f80d Sep 18, 2018

Choose a reason for hiding this comment

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

Yeah, it's pretty easy to insert the alert dialog to ask the user if they are sure they want to delete all the pets.
It's a matter of

  1. creating a few new strings.
  2. coping over the showDeleteConfirmationDialog() block of code.
    (I renamed it to showDeleteAllConfirmationDialog() )
  3. and adding in the showDeleteAllConfirmationDialog(); into the case R.id.action_delete_all_entries:

I'm surprised how easy it was... considering a few lessons ago it seemed pretty hard.
If you get confused just look at what you did in the EditorActivity for the deleting of a single pet.

@xht418
Copy link

@xht418 xht418 commented on 311f80d Nov 18, 2018

Choose a reason for hiding this comment

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

For those who are not seeing changes on the screen, did you put "notifyChange()" in your PetProvider class? Remember to put that method into your "insert()", "update()" and "delete()" helper method as well as the "setNotificationUri()" in query method.

@warnockg90
Copy link

@warnockg90 warnockg90 commented on 311f80d Dec 20, 2018

Choose a reason for hiding this comment

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

I am stuck with my code back when trying to implement the LoaderManager.
I've even copied the code from above and it still doesn't work.

This statement is producing an error:

getLoaderManager().initLoader(PET_LOADER, null, this);

It AndroidStudio it says:

Wrong 3rd argument type.
Found 'com.example.android.petsapp.CatalogActivity'
Required android.app.LoaderManager.LoaderCallbacks<java.lang.object>'

Anyone got any bright ideas?
I have everything imported that I need as far as I know.

@xht418
Copy link

@xht418 xht418 commented on 311f80d Dec 20, 2018

Choose a reason for hiding this comment

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

I am stuck with my code back when trying to implement the LoaderManager.
I've even copied the code from above and it still doesn't work.

This statement is producing an error:

getLoaderManager().initLoader(PET_LOADER, null, this);

It AndroidStudio it says:

Wrong 3rd argument type.
Found 'com.example.android.petsapp.CatalogActivity'
Required android.app.LoaderManager.LoaderCallbacks<java.lang.object>'

Anyone got any bright ideas?
I have everything imported that I need as far as I know.

Try to use "getSupportLoaderManager()" instead of "getLoaderManager()".

@Ruthvikbr
Copy link

Choose a reason for hiding this comment

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

Can anyone tell me why the app crashes when i press delete all apps option

@Abhilashtyagi007
Copy link

Choose a reason for hiding this comment

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

No change in my app whenever I insert data via FAB then toast appears only that is "Pet saved" but home screen shows image / empty view screen .... I am confused don't know what to do ... this code out of my mind.... :(

Please sign in to comment.