我想根据具体日期列出我日历中的事件。我正在使用以下
Cursor cursor = getContentResolver().query(Uri.parse("content://com.android.calendar/events"), new String[]{ "_id", "title", "description", "dtstart", "dtend", "eventLocation" }, null, null, null);
此代码工作正常,但它会带来日历中的总事件。但我需要在2011-08-15等特定日期举办活动。我尝试了以下代码
Cursor cursor = getContentResolver().query(Uri.parse("content://com.android.calendar/events"), new String[]{ "_id", "title", "description", "dtstart", "dtend", "eventLocation" }, "dtstart", new String[]{sdf.format(calender.getTime())}, null);
cursor.moveToFirst();
但我得到例外
引起:android.database.sqlite.SQLiteException:没有这样的列: Calendars.dtstart :,编译时:SELECT _id,title,description, dtstart,dtend,eventLocation FROM view_events WHERE(1)AND (Calendars.dtstart)
请提出解决问题的建议
答案 0 :(得分:0)
尝试此代码我在我的应用程序中我将此事件用于显示事件日期明智;
ContentResolver contentResolver = mContext.getContentResolver();
final Cursor cursor = contentResolver.query(Uri.parse("content://com.android.calendar/calendars"),
(new String[] { "_id", "displayName", "selected" }), null, null, null);
HashSet<String> calendarIds = new HashSet<String>();
while (cursor.moveToNext()) {
final String _id = cursor.getString(0);
final String displayName = cursor.getString(1);
final Boolean selected = !cursor.getString(2).equals("0");
//System.out.println("Id: " + _id + " Display Name: " + displayName + " Selected: " + selected);
calendarIds.add(_id);
}
在这个剪辑中你应该在calendarIds中获取所有日历事件...现在检查
for (String id : calendarIds) {
Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
long now = new Date().getTime();
ContentUris.appendId(builder, now - DateUtils.DAY_IN_MILLIS * 10000);
ContentUris.appendId(builder, now + DateUtils.DAY_IN_MILLIS * 10000);
Cursor eventCursor = contentResolver.query(builder.build(),
new String[] { "title", "begin", "end", "allDay"}, "Calendars._id=" + id,
null, "startDay ASC, startMinute ASC");
// For a full list of available columns see http://tinyurl.com/yfbg76w
while (eventCursor.moveToNext()) {
eventTitle = eventCursor.getString(0);
begin = new Date(eventCursor.getLong(1));
end = new Date(eventCursor.getLong(2));
allDay = !eventCursor.getString(3).equals("0");
eventDate = begin.getDate();
eventTime = begin.getHours();
eventMonth = begin.getMonth();
eventYear = begin.getYear();
event.add(eventTitle);
SimpleDateFormat sdfcur = new SimpleDateFormat("yyyy-MM-dd");
String beginString = sdfcur.format(begin);
Date begindt = null;
try {
begindt = sdfcur.parse(beginString);
stringEventDate = begindt.getDate();
stringEventMonth = begindt.getMonth();
stringEventYear = begindt.getYear();
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Date dt = null;
dt = sdfcur.parse(onClickDate);
int passedday=dt.getDate();
int passedmonth = dt.getMonth();
int passedyear = dt.getYear();
答案 1 :(得分:0)
请参阅此Calendar Provider Documentation此链接
我正在使用此代码,它正在运行..
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(Uri.parse("content://com.android.calendar/calendars"), (new String[] { Calendars._ID, Calendars.ACCOUNT_NAME, Calendars.CALENDAR_DISPLAY_NAME, }), null, null, null);
答案 2 :(得分:0)
我这样做了:
String[] projection = new String[] { CalendarContract.Events.CALENDAR_ID, CalendarContract.Events.TITLE, CalendarContract.Events.DESCRIPTION, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND, CalendarContract.Events.ALL_DAY, CalendarContract.Events.EVENT_LOCATION };
Calendar startTime = Calendar.getInstance();
startTime.set(Calendar.HOUR_OF_DAY,0);
startTime.set(Calendar.MINUTE,0);
startTime.set(Calendar.SECOND, 0);
Calendar endTime= Calendar.getInstance();
endTime.add(Calendar.DATE, 1);
String selection = "(( " + CalendarContract.Events.DTSTART + " >= " + startTime.getTimeInMillis() + " ) AND ( " + CalendarContract.Events.DTSTART + " <= " + endTime.getTimeInMillis() + " ) AND ( deleted != 1 ))";
Cursor cursor = context.getContentResolver().query(CalendarContract.Events.CONTENT_URI, projection, selection, null, null);
List<String> events = new ArrayList<>();
if (cursor!=null&&cursor.getCount()>0&&cursor.moveToFirst()) {
do {
events.add(cursor.getString(1));
} while ( cursor.moveToNext());
}
它将从日历内容提供商处提供今天的活动。
注意:管理来自内容提供商的已删除事件。
完成强>
答案 3 :(得分:0)
这种方法100%为我工作,但我尝试了几种方法。它将获得所有事件并存储在一组中。然后,您可以检查集合是否在集合中。通过这种方式,您可以检查重复事件
public Set<String> readCalendarEvent(Context context) {
Cursor cursor = context.getContentResolver()
.query(
Uri.parse("content://com.android.calendar/events"),
new String[] { "calendar_id", "title", "description",
"dtstart", "dtend", "eventLocation" }, null,
null, null);
cursor.moveToFirst();
// fetching calendars name
String CNames[] = new String[cursor.getCount()];
// fetching calendars id
calendars.clear();
Log.d("cnameslength",""+CNames.length);
if (CNames.length==0)
{
Toast.makeText(context,"No event exists in calendar",Toast.LENGTH_LONG).show();
}
for (int i = 0; i < CNames.length; i++) {
calendars.add(cursor.getString(1));
CNames[i] = cursor.getString(1);
cursor.moveToNext();
}
return calendars;
}