在使用ContentProvider做數(shù)據(jù)庫數(shù)據(jù)查詢時,默認是不處理重復數(shù)據(jù)的,如果你需要過濾重復數(shù)據(jù)厂抖,可以采用Distinct關鍵字输虱,具體操作方法如下:
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
String tableName = null;
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
// 解決查詢數(shù)據(jù)重復的問題。
switch (sUriMatcher.match(uri)) {
case CAGEGORY:
tableName = P.CategoryColumns.TABLE_NAME;
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
qb.setTables(tableName);
// 避免查詢到重復數(shù)據(jù)
qb.setDistinct(true);
Cursor cursor = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
// Tell the cursor what uri to watch, so it knows when its source data changes
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}