Android的 - 在ContentProvider的最大选择 [英] Android - Select max in contentProvider

查看:139
本文介绍了Android的 - 在ContentProvider的最大选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我自定义的ContentProvider运行此查询。

I try to run this query on my custom contentprovider.

cursor = activity.getContentResolver().query(
                GoalDescriptor.CONTENT_URI,
                "max(priority)", null,
                null, null);

要取得优先权INT列的最大值。

to obtain the max value of priority int column.

我也尝试:

cursor = activity.getContentResolver().query(
                GoalDescriptor.CONTENT_URI,
                null, "max(priority)",
                null, null);

丝毫没有成功。

这code返回此异常:

This code return this exception:

    java.lang.IllegalArgumentException: Invalid column MAX(priority)
E/DatabaseUtils(  688):     at android.database.sqlite.SQLiteQueryBuilder.computeProjection(SQLiteQueryBuilder.java:523)
E/DatabaseUtils(  688):     at android.database.sqlite.SQLiteQueryBuilder.buildQuery(SQLiteQueryBuilder.java:370)
E/DatabaseUtils(  688):     at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:323)
E/DatabaseUtils(  688):     at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:280)
E/DatabaseUtils(  688):     at nan.salsa.contentprovider.goal.GoalContentProvider.query(GoalContentProvider.java:118)
E/DatabaseUtils(  688):     at android.content.ContentProvider$Transport.bulkQuery(ContentProvider.java:150)
E/DatabaseUtils(  688):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:111)
E/DatabaseUtils(  688):     at android.os.Binder.execTransact(Binder.java:288)
E/DatabaseUtils(  688):     at dalvik.system.NativeStart.run(Native Method)
D/AndroidRuntime(  727): Shutting down VM

我的内容提供者实现(标准 - 见记事本样品)

My Content Provider Implementation (Standard - see notePad sample)

public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    qb.setTables(GoalDescriptor.TABLE_NAME);

    switch (sUriMatcher.match(uri)) {

    case GOAL:
        qb.setProjectionMap(sGoalProjectionMap);
        qb.appendWhere(GoalDescriptor._ID + "="
                + uri.getPathSegments().get(1));
        break;

    case GOALS:
        qb.setProjectionMap(sGoalProjectionMap);
        break;

    default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }

    // If no sort order is specified use the default
    String orderBy;
    if (TextUtils.isEmpty(sortOrder)) {
        orderBy = GoalInfo.GoalDescriptor.DEFAULT_SORT_ORDER;
    } else {
        orderBy = sortOrder;
    }

    // Get the database and run the query
    SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    Cursor c = qb.query(db, projection, selection, selectionArgs, null,
            null, orderBy);

    // Tell the cursor what uri to watch, so it knows when its source data
    // changes
    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
}

你能帮助我吗?

can you help me ?

关于

推荐答案

您发送的最大值(优先级)的投影,在把它作为选择,它应该工作。

You are sending the max(priority) as the projection, send it in as the selection and it should work.

请参阅这个问题:
安卓:获取列最高值

See this question: Android: Get highest value in column

编辑:

看样子这应该工作:

cursor = activity.getContentResolver().query(
         GoalDescriptor.CONTENT_URI,
          new String[] {"MAX(priority) AS max_priority"}, null,
         null, null);

这篇关于Android的 - 在ContentProvider的最大选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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