Android:ContentResolver 中的 Distinct 和 GroupBy [英] Android: Distinct and GroupBy in ContentResolver

查看:37
本文介绍了Android:ContentResolver 中的 Distinct 和 GroupBy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DISTINCT 和/或 GROUPBY 添加到基于 ContentResolver 的查询的正确方法是什么?

What would be the correct way to add DISTINCT and/or GROUPBY to ContentResolver-based queries?

现在我必须为每个特殊情况创建自定义 URI.

Right now I have to create custom URI for each special case.

有更好的方法吗?

(我仍然编程为 1.5 作为最小公分母)

(I still program for 1.5 as lowest common denominator)

推荐答案

既然没有人来回答,我就说说我是如何解决这个问题的.基本上我会为每种情况创建自定义 URI 并在 selection 参数中传递条件.然后在 ContentProvider#query 中,我将识别案例并根据表名和选择参数构造原始查询.

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.

这是一个简单的例子:

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);

这篇关于Android:ContentResolver 中的 Distinct 和 GroupBy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆