Bostone Bostone - 5 months ago 46
Android Question

Android: Distinct and GroupBy in ContentResolver

What would be a sensible way to add

DISTINCT
and/or
GROUPBY
to
ContentResolver
- based queries. Right now I have to create custom URI for each special case. Is there a better way?
(I still program for 1.5 as lowest common denominator)

Answer

Since no one came to answer I'm just going to tell how I solved this. Basically I would create custom URI for each case and pass the criteria in selection parameter. Then inside ContentProvider#query I would identify the case and construct raw query based on table name and selection parameter.

Here's quick example:

switch (URI_MATCHER.match(uri)) {
    case TYPES:
        table = TYPES_TABLE;
        break;
    case TYPES_DISTINCT:
        return db.rawQuery("SELECT DISTINCT type FROM types", null);
    default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }
    return db.query(table, null, selection, selectionArgs, null, null, null);
Comments