在使用ContentProvider做数据库数据查询时,默认是不处理重复数据的,如果你需要过滤重复数据,可以采用Distinct关键字,具体操作方法如下:
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
String tableName = null;
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
// 解决查询数据重复的问题。
switch (sUriMatcher.match(uri)) {
case CAGEGORY:
tableName = P.CategoryColumns.TABLE_NAME;
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
qb.setTables(tableName);
// 避免查询到重复数据
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;
}