开发者

Get Calendar Event by Date in Android?

I want to list of events from my calendar based on specific date. I am using following

Cursor cursor = getContentResolver().query(Uri.parse("content://com.android.calendar/events"), new String[]{ "_id", "title", "description", "dtstart", "dtend", "eventLocation" }, null, null, null);

This code works fine but it brings the total events from the calendar. But i need a events on specfic date like 2011-08-15. I tried following code

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();   

But I am getting exception

Caused by: android.database.sqlite.SQ开发者_StackOverflow社区LiteException: no such column: Calendars.dtstart: , while compiling: SELECT _id, title, description, dtstart, dtend, eventLocation FROM view_events WHERE (1) AND (Calendars.dtstart)

Please give some advise to solve my problem


I have done this way:

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());
 }

It will give today's event from Calendar content provider.

Note: Managed the deleted events from content provider.

Done


Refer to Calendar Provider Documentation this link

I am using this code and it is working..

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);


This methods is 100% worked for me however i tried several method . It will get all the event and store in a set . Then you can check the set whether a event is in the set or not . In this way you can check duplicate event

 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;
}


try this code i am in my application i am used this event for display event date wise;

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);
                }

in this snipped you should get all calendar event in calendarIds...now check

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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜