2012-04-09 8 views
8

アンドロイドデバイスでカレンダーイベントを最後のセブンデイズにしたいですか?私は初心者ですので、このためのステップバイステップの解決策を教えてください!誰でも助けてくれますか?私は以下のコードを使用しました!アンドロイドデバイスのデバイスカレンダーのイベントリストを取得する方法は?

public class Example { 

    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 
     // user has them selected for display. 

     final Cursor cursor = contentResolver.query(Uri.parse("content://calendar/calendars"), 
       (new String[] { "_id", "displayName", "selected" }), null, null, null); 
     // For a full list of available columns see http://tinyurl.com/yfbg76w 

     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"); 

      Log.v("anim","Id: " + _id + " Display Name: " + displayName + " Selected: " + selected); 
      calendarIds.add(_id); 
     } 

     // 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://calendar/instances/when").buildUpon(); 
      long now = new Date().getTime(); 
      ContentUris.appendId(builder, now - DateUtils.WEEK_IN_MILLIS); 
      ContentUris.appendId(builder, now + DateUtils.WEEK_IN_MILLIS); 

      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()) { 
       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"); 

       Log.v("anim","Title: " + title + " Begin: " + begin + " End: " + end + 
         " All Day: " + allDay); 
      } 
     } 
    } 


} 

null-pointerExceptionが発生しました。

+1

も、あなたは初心者です、私はあなたが「Google検索」:)で初心者ではないと確信してTHIをご確認くださいsはすでに存在していた質問:[Androidカレンダーのイベント](http://stackoverflow.com/questions/4302209/android-calendar-events) –

+0

あなたは私を助ける!私はこのコードを試してみました。 – Hiteshpatel0024

+0

Googleカレンダーアカウントのイベントを取得するにはどうすればよいですか? – Deepak

答えて

33

私の問題は解決し、私はcodeの下に使用 -

public class Utility { 
    public static ArrayList<String> nameOfEvent = new ArrayList<String>(); 
    public static ArrayList<String> startDates = new ArrayList<String>(); 
    public static ArrayList<String> endDates = new ArrayList<String>(); 
    public static ArrayList<String> descriptions = new ArrayList<String>(); 

    public static ArrayList<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 
     nameOfEvent.clear(); 
     startDates.clear(); 
     endDates.clear(); 
     descriptions.clear(); 
     for (int i = 0; i < CNames.length; i++) { 

      nameOfEvent.add(cursor.getString(1)); 
      startDates.add(getDate(Long.parseLong(cursor.getString(3)))); 
      endDates.add(getDate(Long.parseLong(cursor.getString(4)))); 
      descriptions.add(cursor.getString(2)); 
      CNames[i] = cursor.getString(1); 
      cursor.moveToNext(); 

     } 
     return nameOfEvent; 
    } 

    public static String getDate(long milliSeconds) { 
     SimpleDateFormat formatter = new SimpleDateFormat(
       "dd/MM/yyyy hh:mm:ss a"); 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(milliSeconds); 
     return formatter.format(calendar.getTime()); 
    } 
+0

あなたの答えは素晴らしいです。そのid( "calendar_id")を取得する方法を私に電話してください。私はこのように試みたid = cursor.getString(0);すべてのイベントに対して2を与えます。 – AndroidRaji

+0

あなたのために多くのヘルプをリンクしています:http://www.grokkingandroid.com/androids-calendarcontract-provider/ – Hiteshpatel0024

+0

特定の日付または週のカレンダーイベントを取得するにはどうすればよいですか? – Deepak

1

大感謝! ただし、メインのGoogleアカウントと同様に、1つのカレンダーからすべてのイベントを取得できます。利用可能なすべてのカレンダーからどのようにイベントを取得できますか?

内蔵のカレンダーウィジェットには、Gmail、Exchange、Facebookなどのイベントが表示されます。 これらのイベントを取得するにはどうすればいいですか?

日または週の特定のイベントのために、私は特定の日のために、このようなクエリ文で 選択句と選択のArgsを使用してこれを行ったように:

これらが選択されていると引数を定義します。

クエリがどのように見えるか
Uri content = Uri.parse("content://com.android.calendar/events"); 
String[] vec = new String[] { "calendar_id", "title", "description", "dtstart", "dtend", "allDay", "eventLocation" }; 
String selectionClause = "(dtstart >= ? AND dtend <= ?) OR (dtstart >= ? AND allDay = ?)"; 
String[] selectionsArgs = new String[]{"" + dtstart, "" + dtend, "" + dtstart, "1"}; 

そして、この:

ContentResolver contentResolver = context.getContentResolver(); 
    Cursor cursor = contentResolver.query(content, vec, selectionClause, selectionsArgs, null); 
+0

このコードを作成してください。それは大きな助けになるでしょう。 –

関連する問題