0

我有一个日历应用程序。我想添加一个显示当月所有事件的列表视图。

这是我用来循环的代码,但它只显示本月的最后一个事件,而不是所有事件:

    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);
        } 
    }
}
4

1 回答 1

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;
于 2012-09-17T14:38:16.800 回答