Skip to content

Commit

Permalink
1.15 Implement ContentProvider delete() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Beginning Android committed Aug 23, 2016
1 parent a1bcc0c commit 81fe10a
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions app/src/main/java/com/example/android/pets/data/PetProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,6 @@ private Uri insertPet(Uri uri, ContentValues values) {
return ContentUris.withAppendedId(uri, id);
}

@Override
public int delete(Uri uri, String s, String[] strings) {
return 0;
}

@Override
public int update(Uri uri, ContentValues contentValues, String selection,
String[] selectionArgs) {
Expand Down Expand Up @@ -246,4 +241,24 @@ private int updatePet(Uri uri, ContentValues values, String selection, String[]
// Returns the number of database rows affected by the update statement
return database.update(PetEntry.TABLE_NAME, values, selection, selectionArgs);
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Get writeable database
SQLiteDatabase database = mDbHelper.getWritableDatabase();

final int match = sUriMatcher.match(uri);
switch (match) {
case PETS:
// Delete all rows that match the selection and selection args
return database.delete(PetEntry.TABLE_NAME, selection, selectionArgs);
case PET_ID:
// Delete a single row given by the ID in the URI
selection = PetEntry._ID + "=?";
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
return database.delete(PetEntry.TABLE_NAME, selection, selectionArgs);
default:
throw new IllegalArgumentException("Deletion is not supported for " + uri);
}
}
}

7 comments on commit 81fe10a

@3sna
Copy link

@3sna 3sna commented on 81fe10a Jul 25, 2017

Choose a reason for hiding this comment

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

i still confuse about this statement :

selectionArgs

= new String[] { String.valueOf(ContentUris.parseId(uri)) };

Can you please explain to me what happen inside. Thx

@suleymantekin
Copy link

@suleymantekin suleymantekin commented on 81fe10a Jul 25, 2017

Choose a reason for hiding this comment

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

In that statement we extract the Id from ContentUris using parseId method an then store it in SelectionArgs.

Lets say the uri is something like this:

content://com.example.android.pets/pets/5

We call String.valueOf(ContentUris.parseId(uri))

and then we store 5 in our selectionArgs String Array.

Selection: “_ID=?”
SelectionArgs: { “5” }

So the query will be something like DELETE FROM pets WHERE _ID=5;

@sherifhisham
Copy link

Choose a reason for hiding this comment

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

nice work

@ID2GO
Copy link

@ID2GO ID2GO commented on 81fe10a Jul 11, 2018

Choose a reason for hiding this comment

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

Nice piece of code but when I run it on an emulator or phone, it does not delete anything yet....
Anyone any suggestions?

@tinapleez
Copy link

Choose a reason for hiding this comment

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

Hi ID2GO, the app bar menus do not yet have an instruction to delete. Right now it says: // Do nothing for now

@ID2GO
Copy link

@ID2GO ID2GO commented on 81fe10a Jul 22, 2018

Choose a reason for hiding this comment

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

Yeah tinapleez, I realised that later on down the road. But thanx!

@tinapleez
Copy link

Choose a reason for hiding this comment

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

Cool. I figured that, since your post is dated over a week ago. I thought I'd put the comment up for future confused students, because I also was thrown a little bit by the abruptness at the end of the lesson.

Please sign in to comment.