我尝试从特定日历中读出事件。以下是我尝试使用的代码。但我不知道选择的外观如何。我想使用日历 ID 进行选择。如果没有选择(空),它会读出所有日历,但我只想要一个。查询如下所示:
public final Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
selection:
声明要返回哪些行的过滤器,格式化为 SQL WHERE 子句(不包括 WHERE 本身)。传递 null 将返回给定 URI 的所有行。
此代码是第一次尝试选择:
Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, now - DateUtils.DAY_IN_MILLIS);
ContentUris.appendId(builder, now + DateUtils.DAY_IN_MILLIS);
Cursor mCursor = null;
final String[] projection = new String[]
{ CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND};
mCursor = getContentResolver().query(
builder.build(), projection, Instances._ID + calendarID, null, null);
mCursor.moveToFirst();
那就是上面代码的错误:
09-29 00:07:41.800: E/AndroidRuntime(14507): FATAL EXCEPTION: TweetCollectorTimer
09-29 00:07:41.800: E/AndroidRuntime(14507): android.database.sqlite.SQLiteException: no such column: _id3: , while compiling: SELECT title, dtstart, dtend FROM Instances INNER JOIN view_events AS Events ON (Instances.event_id=Events._id) WHERE (begin<=? AND end>=?) AND (_id3)
09-29 00:07:41.800: E/AndroidRuntime(14507): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:179)
09-29 00:07:41.800: E/AndroidRuntime(14507): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
09-29 00:07:41.800: E/AndroidRuntime(14507): at android.content.ContentProviderProxy.query(ContentProviderNative.java:358)
09-29 00:07:41.800: E/AndroidRuntime(14507): at android.content.ContentResolver.query(ContentResolver.java:311)
09-29 00:07:41.800: E/AndroidRuntime(14507): at de.ring.AppService$1.run(AppService.java:96)
09-29 00:07:41.800: E/AndroidRuntime(14507): at java.util.Timer$TimerImpl.run(Timer.java:284)
编辑:
非常感谢,我改成:
mCursor = getContentResolver().query(
builder.build(), projection, Instances.CALENDAR_ID + " = ?",
new String[]{""+calendarID}, null);
使用 CALENDAR_ID 而不仅仅是 _ID 很重要。