2011-08-01 2 views
2

특정 날짜를 기준으로 내 캘린더의 이벤트 목록을 원합니다. 다음을 사용하고 있습니다.Android에서 캘린더 일정을 날짜로 가져 오시겠습니까?

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을 _id를 선택, 제목 (1) 및 (Calendars.dtstart)

내 문제를 해결하기 위해 몇 가지 조언을 부탁드립니다 view_events FROM, 설명, DTSTART, DTEND, eventLocation

+0

Google 캘린더 GData API를 사용하여 사용자의 Google 캘린더에 액세스하십시오. – CommonsWare

답변

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

쿼리에서 시작일과 종료일을 어떻게 사용 했습니까? – Deepak

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

오늘의 일정을 캘린더 콘텐츠 제공 업체에서 제공합니다.

참고 : 콘텐츠 공급자로부터 삭제 된 이벤트를 관리합니다.

완료

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

답변을 수정하고 코드에 설명을 추가하십시오. 코드 만 사용하는 것은 권장하지 않습니다. 문제를 해결하는 코드의 중요한 부분을 설명하십시오. – WebDevBooster

+1

감사합니다. 추가 정보가 있습니다. 질문 할 수있는 것이 있으면 –

관련 문제