我有一个日历应用。我想添加一个列表视图,显示当前月份的所有事件。
这是我用来循环的代码,但它只显示当月的最后一个事件,而不是 ALL 事件:
for(int i = 0; i < _calendar.getActualMaximum(Calendar.DAY_OF_MONTH); i++){
if(isHoliday(i, month, year, date_value))
{
String date= i + " " + getMonthForInt(month);
CalendarEvents events = new CalendarEvents();
final ArrayList<Event> e = new ArrayList<Event>();
e.addAll(events.eventDetails(hijri_date[1], hijri_date[0]));
for (int j = 0; j < e.size(); j++)
{
Event event = e.get(j);
summary_data = new Summary[]
{
new Summary(date, event.eventdetails)
};
}
}
}
summaryAdapter = new SummaryAdapter(this.getActivity().getApplicationContext(), R.layout.listview_item_row, summary_data);
calendarSummary = (ListView) v.findViewById(R.id.calendarSummary);
calendarSummary.setAdapter(summaryAdapter);
更新后的代码:
CalendarEvents events = new CalendarEvents();
final ArrayList<Event> e = new ArrayList<Event>();
String date;
for(int i = 0; i < _calendar.getActualMaximum(Calendar.DAY_OF_MONTH); i++){
if(isHoliday(i, month, year, date_value))
{
date = i + "-" + month + "-" + year;
e.addAll(events.eventDetails(month, day));
summary_data = new Summary[e.size()];
for (int j = 0; j < e.size(); j++)
{
Event event = e.get(j);
summary_data[j] = new Summary(date, event.eventdetails);
}
}
}
答案 0 :(得分:2)
您每次都在创建数组并分配给相同的引用。这就是为什么最后一个替换其他所有东西。
summary_data = new Summary[]
{
new Summary(date, event.eventdetails)
};
您知道前面的大小,因此首先创建大小为数组的数组,然后为索引
指定值summary_data = new Summary[e.size()];
for(....)
{
......
summary_data[j] = new Summary(date, event.eventdetails);
}
/////
if(isHoliday(i, month, year, date_value))
{
String date = i + "-" + month + "-" + year;