CalendarProvider:游标始终为null

时间:2017-08-30 13:00:15

标签: android android-calendar

所以我试图从特定的日历ID获取今天的每个事件。在编写,更新和删除事件时我没有遇到任何问题,但我无法让阅读工作。

这是我的代码:

    String[] query= new String[]{
            Events.CALENDAR_ID,
            Events.TITLE,
            Events.DESCRIPTION,
            Events.DTSTART,
            Events.HAS_ATTENDEE_DATA,
            Attendees.ATTENDEE_NAME,
            Attendees.ATTENDEE_EMAIL
    };

    String selection = "(( " + Events.DTSTART + " >= " + today.getTime() + " ))";

    if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        return null;
    }
    Cursor cur = activity.getBaseContext().getContentResolver().query(Events.CONTENT_URI, query, selection, null, null);

    Log.d("CalendarUtility","is cursor null : "+Boolean.toString(cur==null)); // always prints true

我试过没有最小日期,但我的光标仍为空。 以防万一,因为API更新会改变一切: compileSdkVersion 25minSdkVersion 21

1 个答案:

答案 0 :(得分:0)

我发现为什么它不起作用。我的查询尝试访问CalendarContract.AttendeesAttendees.ATTENDEE_NAMEAttendees.ATTENDEE_EMAIL),但我的Cursor意味着返回CalendarContract.Events表。

所以这就是我所做的:

private static String[] EVENTS_QUERY = new String[]{
        Events._ID,
        Events.CALENDAR_ID,
        Events.TITLE,
        Events.DESCRIPTION,
        Events.DTSTART,
        Events.HAS_ATTENDEE_DATA
        // No Attendees columns
};

private static String EVENTS_SELECTION = "(( " + Events.DTSTART + " >= " + nowTimeInMillis + " ))";

//...


Cursor cur = activity.getContentResolver().query(Events.CONTENT_URI, EVENTS_QUERY, EVENTS_SELECTION, null, null);

之后,如果我的活动有与会者数据:

String[] ATTENDEES_QUERY = new String[]{
                                Attendees.EVENT_ID,
                                Attendees.ATTENDEE_NAME,
                                Attendees.ATTENDEE_EMAIL
                        };
String ATTENDEES_SELECTION =  "(( " + Attendees.EVENT_ID + " == " + eventId + " ))";
Cursor att = activity.getContentResolver().query(Attendees.CONTENT_URI,ATTENDEES_QUERY,ATTENDEES_SELECTION,null,null);