Search code examples
androidandroid-contentprovider

Content provider doesn't delete row


Weird thing is my content provider doesn't delete the row I've asked to. As far as I see it should work and I don't understand why it doesn't.

This is the delete method in my provider:

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    SQLiteDatabase db = helper.getWritableDatabase();

    switch (uriMatcher.match(uri)) {
    case SINGLE_ROW:
        String rowID = uri.getPathSegments().get(1);

        selection = KEY_ROWID
                + "="
                + rowID
                + (!TextUtils.isEmpty(selection) ? " AND (" + selection
                        + ')' : "");
        Log.i("Selection", "" +  selection);
        break;
    case ALLROWS:
        Log.i("Deleted", "All rows");
        break;

    default:
        Log.i("Switch case", "default value");
        break;
    }

    if (selection == null) {
        selection = "1";
    }

    int deleteCount = db.delete(helper.DATABASE_TABLE, selection,
            selectionArgs);
    Log.i("Deleted rows",
            "" + db.delete(helper.DATABASE_TABLE, selection, selectionArgs));
    getContext().getContentResolver().notifyChange(uri, null);

    return deleteCount;
}

Here is the Log output which indicates no row was deleted which makes no sense to me. can anyone help?

03-24 22:36:34.809: I/Selection(27648): _id=2
**03-24 22:36:34.809: I/Deleted rows(27648): 0**
03-24 22:36:34.814: I/Delete Uri(27648): content://com.shifts.provider/shiftsTEST/2

Solution

  • I've found the issue myself - I've refactored the method like this:

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = helper.getWritableDatabase();
    
    
        int deleteCount = db.delete(helper.DATABASE_TABLE, KEY_ID + "="
                + Integer.valueOf(selection), null);
        Log.i("Selection", "" + selection);
    
        getContext().getContentResolver().notifyChange(uri, null);
    
        return deleteCount;
    }