Finds an event from Calendar based on the ID - Android Android OS

Android examples for Android OS:Calendar Event

Description

Finds an event from Calendar based on the ID

Demo Code


import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Attendees;
import android.provider.CalendarContract.Calendars;
import android.provider.CalendarContract.Events;
import android.provider.CalendarContract.Reminders;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;

public class Main{
    /** The main/basic URI for the android events table */
    private static final Uri EVENT_URI = CalendarContract.Events.CONTENT_URI;
    /**// w w  w  . j  a va2  s  .co m
     * Finds an event based on the ID
     * 
     * @param ctx
     *            The context (e.g. activity)
     * @param id
     *            The id of the event to be found
     */
    public static CalendarRequest getEventByID(CalendarRequest crx) {
        ContentResolver cr = crx.getContext().getContentResolver();
        // Projection array for query (the values you want)
        final String[] PROJECTION = new String[] { Events._ID,
                Events.TITLE, Events.DESCRIPTION, Events.EVENT_LOCATION,
                Events.DTSTART, Events.DTEND, };
        final int TITLE_INDEX = 1, DESC_INDEX = 2, LOCATION_INDEX = 3, START_INDEX = 4, END_INDEX = 5;
        long start_millis = 0, end_millis = 0;
        String title = null, description = null, location = null;
        final String selection = "(" + Events.OWNER_ACCOUNT + " = ? AND "
                + Events._ID + " = ?)";
        final String[] selectionArgs = new String[] { crx.getAccountName(),
                crx.getEventID() + "" };
        Cursor cursor = cr.query(buildEventUri(crx.getAccountName()),
                PROJECTION, selection, selectionArgs, null);
        // at most one event will be returned because event ids are unique in
        // the table
        if (cursor.moveToFirst()) {
            title = cursor.getString(TITLE_INDEX);
            description = cursor.getString(DESC_INDEX);
            location = cursor.getString(LOCATION_INDEX);
            start_millis = cursor.getLong(START_INDEX);
            end_millis = cursor.getLong(END_INDEX);
            // do something with the values...
            crx.setTitle(title);
            crx.setDescription(description);
            crx.setDateStart(start_millis);
            crx.setDateEnd(end_millis);
            crx.setLocation(location);
        }
        cursor.close();
        return crx;
    }
    /** Builds the Uri for events (as a Sync Adapter) */
    private static Uri buildEventUri(String accountName) {
        return EVENT_URI
                .buildUpon()
                .appendQueryParameter(
                        CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                .appendQueryParameter(Calendars.ACCOUNT_NAME, accountName)
                .appendQueryParameter(Calendars.ACCOUNT_TYPE,
                        CalendarContract.ACCOUNT_TYPE_LOCAL).build();
    }
}

Related Tutorials