2011-08-01 11 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年8月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)AND (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); 
       } 

あなたは

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

をチェックし、今... calendarIds内のすべてのカレンダーイベントを取得する必要が切り取ら私はこのコードを使用していますし、それが働いている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

ありがとう私は詳細を追加しました。ご質問がありましたらお聞かせください –

関連する問題