查询单行时的Android emty游标

时间:2017-09-06 07:27:41

标签: android sqlite android-sqlite android-cursor android-cursorloader

仅当从数据库中查询单行时,游标才为空。当查询匹配所有表时,游标返回正确的信息。从表中查询单行时提供的附加参数是" selection"和" selectionArgs"他们都是正确的。问题应该在Uri的最后一段,但尝试从Uri获取最后一段的不同方法(总是正确的值)并将他添加到selectionArgs []仍然没有改变任何东西,光标是空的。

错误应该在哪里?

@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
    SQLiteDatabase db = mDbHelper.getReadableDatabase();
    Cursor cursor;
    int match = sUriMatcher.match(uri);
    switch (match){
        case LIST:
            cursor = db.query(
                    ListEntry.TABLE_NAME,
                    projection,
                    selection,
                    selectionArgs,
                    null,
                    null,
                    sortOrder);
            break;
        case LIST_ENTRY_ID:
            selection = ListEntry._ID + "=?";
            selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
            cursor = db.query(
                    ListEntry.TABLE_NAME,
                    projection,
                    selection,
                    selectionArgs,
                    null,
                    null,
                    sortOrder);
            break;
        default:
            throw new IllegalArgumentException("Cannot query unknown URI " + uri);
    }
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    if (cursor == null){
        return null;
    } else {
        return cursor;
    }

0 个答案:

没有答案