[英]android.content.ContentResolver.query NoSuchMethodError
[英]Android Stock Calendar or android.content.ContentResolver.query() ignores selectionArgs
我正在使用舊設備上的非官方 API 從Stock Google Calendar
獲取事件。 現在我正在重構代碼以使用android.content.ContentResolver.query()
selection
和selectionArgs
而不是僅手動綁定selection
(它生成有效的 SQL 並在我的測試設備 [2.2 和 2.3.3] 上正常工作)。
問題:如果我立即綁定參數(例如: deleted = 0
),我的測試應用程序會返回預期的事件,但是當 Android 綁定時(例如: deleted = ?
with String[] { "0" }
沒有返回任何事件。原因是什么忽略selectionArgs
?
LogCat 沒有顯示錯誤。
示例(預期)
STATIC: cursor=225
DYNAMIC: cursor=255
返回了 255 個事件,因為我的日歷今年發生了 255 個事件。
示例(測試用例)
測試:摩托羅拉里程碑,CyanogenMod 7 (Android 2.3.3)。
STATIC: cursor=225
DYNAMIC: cursor=0
我假設(但希望我做錯了什么)Android 或日歷應用程序在綁定時失敗。 我發現了一個有類似問題的錯誤報告,但我不確定日歷應用程序是否受到影響: 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.