Вопрос

I am trying to create a app which will fetch details of Events and Attendees from Calendar app on device.

I am facing the problems which are:

1). In many of the events Title and their attendees does not match.

2). In many of the events I am getting 0 attendees (mainly for upcoming events).

Here is my code: (Please let me know the mistake).

public class ReadCalendar {
static Cursor cursor;

public static void readCalendar(Context context) {

ContentResolver contentResolver = context.getContentResolver();

// Fetch a list of all calendars synced with the device, their display names and whether the

cursor = contentResolver.query(Uri.parse("content://com.android.calendar/calendars"),
            (new String[] { Calendars._ID, Calendars.NAME}), null, null, null);

HashSet<String> calendarIds = new HashSet<String>();

try
{
    System.out.println("Count="+cursor.getCount());
    if(cursor.getCount() > 0)
    {
        System.out.println("the control is just inside of the cursor.count loop");
    while (cursor.moveToNext()) {

         String _id = cursor.getString(0);
         String displayName = cursor.getString(1);
         //Boolean selected = !cursor.getString(2).equals("0");

        System.out.println("Id: " + _id + " Display Name: " + displayName);
        calendarIds.add(_id);
    }
}
}
catch(AssertionError ex)
{
    ex.printStackTrace();
}
catch(Exception e)
{
    e.printStackTrace();
}


// For each calendar, display all the events from the previous week to the end of next week.        
for (String id : calendarIds) {
    Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
    //Uri.Builder builder = Uri.parse("content://com.android.calendar/calendars").buildUpon();
    long now = new Date().getTime();

    ContentUris.appendId(builder, now - DateUtils.DAY_IN_MILLIS * 10000);
    ContentUris.appendId(builder, now + DateUtils.DAY_IN_MILLIS * 10000);

    Log.e("123", "Calender ID---->>>>>>"+id);
Cursor eventCursor = contentResolver.query(builder.build(),
            new String[]  { Events.TITLE, "begin", "end", "allDay", Events._ID, Events.CALENDAR_ID}, Events.CALENDAR_ID+"=" + id,
            null, "_id ASC");

    Log.e("123","eventCursor count====="+eventCursor.getCount());
    if(eventCursor.getCount()>0)
    {

        if(eventCursor.moveToFirst())
        {
            do
            {
                Object mbeg_date,beg_date,beg_time,end_date,end_time;

                final String title = eventCursor.getString(0);
                final Date begin = new Date(eventCursor.getLong(1));
                final Date end = new Date(eventCursor.getLong(2));
                final Boolean allDay = !eventCursor.getString(3).equals("0");
                final String eventId = eventCursor.getString(4);
                final String calendarID = eventCursor.getString(5);

                Log.e("123", "Event Id----->>>>>"+eventId+"---------calendarId----->>>"+calendarID);

        /*  System.out.println("Title: " + title + " Begin: " + begin + " End: " + end +
                    " All Day: " + allDay);
        */  
                Log.e("123","Title:"+title);
                Log.e("123","Begin:"+begin);
                Log.e("123","End:"+end);
                Log.e("123","All Day:"+allDay);

             // Attendees Code
                Cursor eventAttendeesCoursor = contentResolver.query(CalendarContract.Attendees.CONTENT_URI, new String []{ Attendees.ATTENDEE_NAME, Attendees.EVENT_ID}, Attendees.EVENT_ID +" = " + eventId, null, null);
                Log.e("123", "Count of no of attendees-----"+eventAttendeesCoursor.getCount());
                if(eventAttendeesCoursor.getCount()>0)
                {

                    if(eventAttendeesCoursor.moveToFirst())
                    {
                        do {
//                              Log.e("123", "Attendees Name---->>>"+ eventAttendeesCoursor.getString(0));
                            Log.e("123", "Attendees Event ID---->>>"+ eventAttendeesCoursor.getString(1));
                        } while(eventAttendeesCoursor.moveToNext());
                    }
                }

            }
            while(eventCursor.moveToNext());
        }
    }
    break;
}
}
}
Это было полезно?

Решение

I didnt look in details into your code, but i just ran into the same issue - and it was because of a mixup between the instance id and the event id. I did see that your uri (builder) is based on instances, but the field you're requiring is event.id : these are distinct.

The important thing is that the attendees table is based on the EVENT id - and not the instance id - so it's likely to explain your issues. Put it all in order, consistently (i.e. extracting the EVENT id from the INSTANCE table based on your URI and pass it on to the ATTENDEES table. See if that helps.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top