Group By in ContentResolver in Ice Cream Sandwich

后端 未结 3 662
春和景丽
春和景丽 2020-12-06 05:19

I am making a query on the Android Contacts ContentProvider. I need a Group By clause. In Gingerbread and Honeycomb, I do something like this to search phone numbers and ema

3条回答
  •  [愿得一人]
    2020-12-06 05:44

    first off all excuse my POOR English! I'm new to Java/Android, started with 4.2.1 and fight with that too almost 2 days, then i start reading some more details about SQLiteQueryBuilder the query part is pretty much that what u are looking for ;)

    it have:

    public Cursor query (SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder)
    

    the query "function" of the Content Provider only gives you:

    query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder)
    

    here u can trick around, i will post you my code snip:

        @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        final SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    /* a String is a Object, so it can be null!*/
        String groupBy = null;
        String having = null;
    
        switch (sUriMatcher.match(uri)) {
    ...
    ...
    ...
            case EPISODES_NEXT:
            groupBy = "ShowID";
            queryBuilder.setTables(EpisodenTable.TableName);
            break;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
    
        Cursor c = queryBuilder.query(db, projection, selection, selectionArgs,
                groupBy, having, sortOrder);
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
    }
    

    thats its!

    here the code i use to execute:

            Cursor showsc = getContext().getContentResolver().query(
                WhatsOnTVProvider.CONTENT_EPISODES_NEXT_URI,
                EpisodenTable.allColums_inclCount,
                String.valueOf(Calendar.getInstance().getTimeInMillis() / 1000)
                        + " < date", null, null);
    

提交回复
热议问题