read Calendar Event - Android Android OS

Android examples for Android OS:Calendar Event

Description

read Calendar Event

Demo Code


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.graphics.Color;
import android.net.Uri;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Calendars;
import android.provider.CalendarContract.Events;
import android.provider.CalendarContract.Instances;
import android.text.format.Time;

public class Main{
    private static Map<String, String> item;
    private static ArrayList<Map<String, String>> data;
    public static final String[] INSTANCES_PROJECTION = new String[] {
            Instances.EVENT_ID, Instances.TITLE, Instances.START_DAY };
    public static final int INSTANCES_ID_INDEX = 0,
            INSTANCES_TITLE_INDEX = 1, INSTANCES_START_DAY_INDEX = 2;
    public static ArrayList<Map<String, String>> readCalendarEvent(
            final Context context, final String eventDate) {
        data = new ArrayList<Map<String, String>>();
        Cursor cursor = null;/* w ww . j a  v a2  s. com*/
        final String getDataMilliSeconds = DataProcess
                .getDataMilliSeconds(eventDate);
        try {
            cursor = getCursor(context, getDataMilliSeconds);
            if (cursor != null && cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    item = new HashMap<String, String>();
                    item.put(
                            INSTANCES_PROJECTION[INSTANCES_ID_INDEX],
                            (cursor.getString(INSTANCES_ID_INDEX) != null) ? cursor
                                    .getString(INSTANCES_ID_INDEX) : "");
                    item.put(
                            INSTANCES_PROJECTION[INSTANCES_TITLE_INDEX],
                            (cursor.getString(INSTANCES_TITLE_INDEX) != null) ? cursor
                                    .getString(INSTANCES_TITLE_INDEX) : "");
                    item.put(
                            INSTANCES_PROJECTION[INSTANCES_START_DAY_INDEX],
                            (cursor.getString(INSTANCES_START_DAY_INDEX) != null) ? cursor
                                    .getString(INSTANCES_START_DAY_INDEX)
                                    : "");
                    data.add(item);
                }
            }
        } catch (SQLException e) {
            System.out
                    .println("[CalendarIntentHelper][readCalendarEvent]SQLException:"
                            + e);
        } catch (Exception ex) {
            System.out
                    .println("[CalendarIntentHelper][readCalendarEvent]Exception:"
                            + ex);
        }
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        return data;
    }
    private static Cursor getCursor(final Context context,
            final String getDataMilliSeconds) {
        Cursor cursor = null;
        String selection[] = null;
        final String where = INSTANCES_PROJECTION[INSTANCES_START_DAY_INDEX]
                + " = ?";
        //
        selection = new String[] { String.valueOf(Time.getJulianDay(
                Long.valueOf(getDataMilliSeconds), 0)) };
        cursor = context.getContentResolver().query(
                getInstancesUri(getDataMilliSeconds), INSTANCES_PROJECTION,
                where, selection, null);
        return cursor;
    }
    private static Uri getInstancesUri(final String getDataMilliSeconds) {
        final Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
        // StartDate
        ContentUris.appendId(builder, Long.valueOf(getDataMilliSeconds));
        // EndDate
        ContentUris.appendId(builder, Long.valueOf(getDataMilliSeconds));
        return builder.build();
    }
}

Related Tutorials