内容提供商不会删除行

时间:2013-03-24 20:38:24

标签: android android-contentprovider

奇怪的是我的内容提供商没有删除我要求的行。据我所知它应该工作,我不明白为什么它不。

这是我的提供商中的删除方法:

@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;
}

这是日志输出,表示没有删除行,这对我没有意义。 有人可以帮忙吗?

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

1 个答案:

答案 0 :(得分:1)

我自己发现了这个问题 - 我已经重构了这样的方法:

@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;
}