CalendarProvider:游标始终为空
CalendarProvider : cursor always null
所以我试图从特定的日历 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 25
、minSdkVersion 21
我找到了它不起作用的原因。我的查询尝试访问 CalendarContract.Attendees
(使用 Attendees.ATTENDEE_NAME
和 Attendees.ATTENDEE_EMAIL
),但我的 Cursor 是为了 return CalendarContract.Events
table.
所以这就是我所做的:
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);
所以我试图从特定的日历 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 25
、minSdkVersion 21
我找到了它不起作用的原因。我的查询尝试访问 CalendarContract.Attendees
(使用 Attendees.ATTENDEE_NAME
和 Attendees.ATTENDEE_EMAIL
),但我的 Cursor 是为了 return CalendarContract.Events
table.
所以这就是我所做的:
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);