我正在尝试使用Calendar API来阅读我的Google日历活动及其开始/结束日期/时间 . 代码似乎工作正常,但有一个我无法理解的问题 . nameOfEvent arraylist和startDates / endDates具有完全不同的大小!名称是771项长,开始/结束只有680.如何有没有开始/结束日期的日历项(事件名称)? Log.d崩溃是因为它说startDates超出范围 . 当我调试并查看arraylist大小完全关闭!
这是我的代码:
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++) {
try {
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);
} catch (NumberFormatException e) {
}
cursor.moveToNext();
}
int item=0;
cursor.moveToFirst();
while (cursor.moveToNext()) {
// Read the calendar item, and start/finish time and print
Log.d ("CALENDAR", "Name = " + nameOfEvent.get(item) + " Start Date = " + startDates.get(item) + " End date = " + endDates.get(item));
item++;
}
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());
}
有人可以帮我理解这里的问题是什么,我做错了什么?谢谢