0

我尝试读出所有现有的日历。

我想要一个包含所有标题和帐户的 HashMap。有人可以帮我吗?这是我的尝试。在 LogCat 中,我看到了所有日历。

public HashMap<String, String> readCalendar(Context context) {

    String[] EVENT_PROJECTION = new String[] {                  
        Calendars.ACCOUNT_NAME,                  
        Calendars.CALENDAR_DISPLAY_NAME                                   
    };

    Cursor managedCursor = null;
    HashMap<String, String> calendars = new HashMap<String, String>();
        Uri calendarUri = CalendarContract.Calendars.CONTENT_URI;
        ContentResolver cr = context.getContentResolver();
        managedCursor = cr.query(calendarUri, EVENT_PROJECTION, null, null, null);

        if (managedCursor != null && managedCursor.moveToFirst()) {


            do {
                    // here I see all accounts and names in the LogCat

                for (int i = 0; i < managedCursor.getColumnCount(); i++) {
                    Log.i(LOG_TAG, managedCursor.getColumnName(i) + "="
                            + managedCursor.getString(i));

                }

                String title = managedCursor.xxxxxx;   // I don't know how
                String account = managedCursor.xxxxx;  // I don't know how


                calendars.put(title, account);

            } while (managedCursor.moveToNext());

            managedCursor.close();
        } else {
            Log.i(LOG_TAG, "No Calendars");
                }
 return calendars;
    }
4

1 回答 1

1

您似乎正在日历表中查找列名。它们在 API(日历列部分)中进行了描述。调用可能如下所示(如果这些是您想要的列):

String title = managedCursor.getString(managedCursor.getColumnIndex(
  CalendarContract.Calendars.NAME));
String account = managedCursor.getString(managedCursor.getColumnIndex(
  CalendarContract.Calendars.ACCOUNT_NAME));
于 2012-06-17T13:32:41.137 回答