2012-10-04 64 views
3

我正在Android版本4中顯示日曆事件,它可以爲這樣的事件的標題和其他詳細信息給出數組列表,但無法顯示。
我收到錯誤日曆未找到在特定日期獲取ICS android版本4的日曆事件

public void readCalendar1() 
{ 

    ContentResolver contentResolver = this.getContentResolver(); 

    // Fetch a list of all calendars synced with the device, their display names and whether the 
    // user has them selected for display. 

    Cursor cursor; 
    cursor = contentResolver.query(CalendarContract.Calendars.CONTENT_URI, new String[] 
      { CalendarContract.Calendars._ID, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME }, 
      null, null, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME + " ASC"); 

    HashSet<String> calendarIds1 = new HashSet<String>(); 

    while (cursor.moveToNext()) 
    { 

     final String _id = cursor.getString(0); 
     final String displayName = cursor.getString(1); 

     System.out.println("Id: " + _id + " Display Name: " + displayName); 
     calendarIds1.add(_id); 
    } 

    // For each calendar, display all the events from the previous week to the end of next week.   
    for (String id : calendarIds1) 
    { 
     Uri builder = CalendarContract.Events.CONTENT_URI; 
     System.out.println("str in read cal1 "+strconvert+"str2 in read cal1 "+strconvert1); 

//strconvert and strconvert1 are string which have the particular dates 



     Cursor eventCursor = contentResolver.query(builder, 
       new String[] { CalendarContract.Events.TITLE, strconvert, 
      strconvert1, CalendarContract.Events.ALL_DAY,CalendarContract.Events.EVENT_LOCATION, 
      CalendarContract.Events.HAS_ALARM,CalendarContract.Events.DESCRIPTION}, 
       "calendar_id=" + id,null,"dtstart ASC"); 
     // For a full list of available columns see http://tinyurl.com/yfbg76w 
     int n=eventCursor.getCount(); 
     System.out.println("No. of rows is="+n); 
     while(eventCursor.moveToNext()) 
     { 
      title = eventCursor.getString(0); 
      begin = new Date(eventCursor.getLong(1)); 
      end = new Date(eventCursor.getLong(2)); 
      allDay = !eventCursor.getString(3).equals("0"); 
      loc=eventCursor.getString(4); 
      hasalarm = !eventCursor.getString(5).equals("0"); 
      desc=eventCursor.getString(6); 

       titlestr.add(title); 
       sdatestr.add(begin.toString()); 
       edatestr.add(end.toString()); 
       locstr.add(loc); 
       descstr.add(desc); 
       alarmstr.add(hasalarm.toString()); 
       System.out.println("Title String: " + titlestr); 
       System.out.println("Begin String: " + sdatestr); 
       System.out.println("End String: " + edatestr); 
       System.out.println("Loc String: " + locstr); 
       System.out.println("Desc String: " + descstr); 
       System.out.println("Alarm String: " + alarmstr); 
//    } 

      System.out.println("Title: " + title + " Begin: " + begin + " End: " + end + 
        " All Day: " + allDay+" Location="+loc+" Descriptn="+desc); 
     } 
    } 




} 

回答

2

此代碼爲我工作,爲下面的IC安卓4.0及以上的IC

//for os version android bELOW version 4(ICS) 


public static boolean eventChecker(Context context,ContentResolver cr,String calID){ 


     Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon(); 
     long now = new Date().getTime(); 
     ContentUris.appendId(builder, now); 
     ContentUris.appendId(builder, now + DateUtils.YEAR_IN_MILLIS); 





     Cursor eventCursorr = cr.query(builder.build(), 
       new String[] { "title", "begin","description"}, "Calendars._id=" + calID, 
       null, "startDay ASC, startMinute ASC"); 

     while (eventCursorr.moveToNext()) { 
      final String titler = eventCursorr.getString(0).trim(); 
      final Date beginr = new Date(eventCursorr.getLong(1)); 
      final String descriptionr = eventCursorr.getString(2).trim(); 



      SimpleDateFormat sdfrr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

      String stimesr = sdfrr.format(beginr); 


    System.out.println("titler "+titler +"stimesr "+stimesr +"descriptionr "+descriptionr); 






     } 





    } 

//for os version android version 4(ICS) AND ABOVE 

    @TargetApi(14) 
    public static boolean eventChecker14(Context context,ContentResolver contentResolver,String calID){ 



     Uri builder = CalendarContract.Events.CONTENT_URI; 


      String[] COLS = new String[]{ CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART,CalendarContract.Events.DESCRIPTION}; 


      Cursor eventCursor = contentResolver.query(builder, 
        COLS, 
        "calendar_id=" + calID,null,"dtstart ASC"); 
      int n=eventCursor.getCount(); 

      System.out.println("No. of rows is="+n); 

      while(eventCursor.moveToNext()) 
      { 
       String title1 = eventCursor.getString(0).trim(); 
       Date begin1 = new Date(eventCursor.getLong(1)); 
       String desc1=eventCursor.getString(2).trim(); 

       SimpleDateFormat sdfrr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

       String stimesr = sdfrr.format(begin1); 




       System.out.println("title1"+title1+"desc1"+desc1+"stimesr"+stimesr); 


      } 



    } 
+0

這兩個版本是它做工精細豆形軟糖中讀?獲取異常:android.database.sqlite.SQLiteException:沒有這樣的列:Calendars._id(code 1): –

+0

是的,它是因爲你試圖讓一個colum沒有創建..請檢查你是否錯過了任何逗號或任何東西碼? –

+0

錯誤消息說** Calendars._id **不存在。在JellyBean上測試它 –

相關問題