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

毎回配列を作成し、同じ参照に割り当てています。そのため、最後の1つが他のすべてを置き換えます。

 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 に答える