我正在尝试使用contentprovider查询和CursorLoader从我的数据库中检索不同的值。虽然CursorLoader不允许不同的规范,但我发现了setDistinct方法,可以将其添加到查询构建器中以添加此规范。我没有找到想要的结果,并且好奇为什么。我的查询如下所示
@Override
public Cursor query(Uri uri, String[] projection,
String selection, String[] selectionArgs, String sortOrder) {
// create SQLiteQueryBuilder for querying flower table
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(Flower.TABLE_NAME);
queryBuilder.setDistinct(true);
switch (uriMatcher.match(uri)) {
case OneFlower: // contact with specified id will be selected
queryBuilder.appendWhere(
Flower._ID + "=" + uri.getLastPathSegment());
break;
case CONTACTS: // all contacts will be selected
break;
default:
throw new UnsupportedOperationException(
getContext().getString(R.string.invalid_query_uri) + uri);
}
Cursor cursor = queryBuilder.query(dbHelper.getReadableDatabase(),
projection, selection, selectionArgs, null, null, sortOrder);
// configure to watch for content changes
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
而我的CursorLoader看起来像这样
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
switch (id) {
case LOADER_ID:
return new CursorLoader(
getContext(),
DatabaseDescription.Flower.CONTENT_URI,
FROM_COLUMNS,
COLUMN_LOCATION + "<> ''",
null,
COLUMN_LOCATION + " ASC"
);
default:
if (BuildConfig.DEBUG)
throw new IllegalArgumentException("no id handled!");
return null;
}
}