简体   繁体   中英

Iterating through current month to get all events

I have a calendar app. I want to add a listview which displays all the events for the current month.

This is the code which I am using to loop but it displays only the last event of the month, instead of ALL the events:

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

UPDATED CODE:

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

You are creating array every time and assigning to same reference. That is why last one replacing everything else.

 summary_data = new Summary[]
                {
                   new Summary(date, event.eventdetails)
                };

You know the size ahead, so create array with size first and then assign values to index

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM