Android Stock Calendar 或 android.content.ContentResolver.query() 忽略 selectionArgs

标签 android sql android-cursor

我在旧设备上使用非官方 API 从 Stock Google Calendar 获取事件。现在我正在重构代码以使用 android.content.ContentResolver.query()selectionselectionArgs 而不是仅手动绑定(bind) 选择(生成有效的 SQL 并在我的测试设备 [2.2 和 2.3.3] 上正常工作)。

问题:如果我立即绑定(bind)参数(例如:deleted = 0),我的测试应用程序返回预期的事件,但是当 Android 绑定(bind)时(例如:deleted = ? with String[] { "0"} 没有返回任何事件。忽略 selectionArgs 的原因是什么?

LogCat 显示没有错误。


示例(预期)

STATIC: cursor=225
DYNAMIC: cursor=255

返回 255 个事件,因为我的日历今年有 255 个事件发生。

示例(测试用例)

测试于:Motorola Milestone、CyanogenMod 7 (Android 2.3.3)。

STATIC: cursor=225
DYNAMIC: cursor=0

我假设(但希望我做错了什么)Android 或日历应用程序无法绑定(bind)。我发现了一个有类似问题的错误报告,但我不确定日历应用程序是否受到影响:http://code.google.com/p/android/issues/detail?id=4467

代码

package com.example;

import java.util.Calendar;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;


/**
 * Related bugs?
 *
 *   - http://code.google.com/p/android/issues/detail?id=4467
 */
public class CalendarCursorTestActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        Calendar begin = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
                 end.add(Calendar.YEAR, 1);

        String uriPrefix = Build.VERSION.SDK_INT >= 8 ? "content://com.android.calendar" : "content://calendar";
        Uri uri = Uri.parse(uriPrefix + "/instances/when/" + begin.getTimeInMillis() + "/" + end.getTimeInMillis());

        String[] projection = null;
        String sortOrder = null;

        // test static - log() returns: STATIC: cursor=225 => 225 events found
        {
            String selection = "deleted = 0";
            String[] selectionArgs = null;

            log("STATIC: ", getBaseContext().getContentResolver().query(
                uri, projection, selection, selectionArgs, sortOrder));
        }

        // test dynamic - log() returns: DYNAMIC: cursor=0 => no events found (!)
        {
            String selection = "deleted = ?";
            String[] selectionArgs = new String[] { "0" };

            log("DYNAMIC: ", getBaseContext().getContentResolver().query(
                uri, projection, selection, selectionArgs, sortOrder));
        }
    }


    public void log (String title, Cursor c) {
        Log.d(getClass().getName(), String.format(
            "%s cursor=%s", title, c == null ? "null" : c.getCount()));
    }
}

最佳答案

CalendarProvider 似乎在这里有一个错误。

query 的实现简单地忽略了用户提供的 selectionArgs 参数。

如果比较 Android 2.3.3 中的实现和 Android 4.0.3您会注意到他们添加了 selectionArgs 作为参数,并构建了他们自己的和用户提供的 combine

错误已在此处修复:Support cleanup of Calendar.java

关于Android Stock Calendar 或 android.content.ContentResolver.query() 忽略 selectionArgs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10289302/

相关文章:

当使用窗口函数并且谓词包含变量时,SQL Server 使用扫描而不是搜索

mysql - 这个sql命令有什么语法错误?

android - 具有相同列名的游标 JOIN

java - 假设连接丢失,如何停止 HTTP 连接?

android - 三星 Galaxy A5 中的自定义对话框未显示在来电屏幕上

android - 一段时间后服务停止工作。需要连续工作

android - 使用 Content Provider 更新数据库

android - 如何在 API 级别 11 之后用 Cursor 填充 Spinner?

android - 表格布局和游标

android - 无法再启动任何模拟器