选择日历事件查询

时间:2012-09-28 22:15:44

标签: android calendar

我尝试从特定日历中读出事件。以下是我尝试使用的代码。但我不知道选择的样子。我想使用日历ID进行选择。如果没有选择(null),它会读出所有日历,但我只想要一个。 查询如下所示:

public final Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) 

的选择:
一个过滤器,声明要返回哪些行,格式化为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。

1 个答案:

答案 0 :(得分:2)

制作本

  getContentResolver().query(
                    builder.build(), projection, Instances._ID  + "=?",
new String[]{""+calendarID}, null);

而不是

getContentResolver().query(
                builder.build(), projection,
 Instances._ID  + calendarID, null, null);

因为第3个参数接受选择字符串并且你传递了这个Instances._ID + calendarID它将被评估为

SELECT ........ WHERE _id3; 

它将日历ID的编号附加到列名称。