2012-09-17 67 views
0

我有一個日曆應用程序。我想添加一個顯示當前月份所有事件的列表視圖。遍歷本月獲取所有事件

這是我使用的環路代碼,但它僅顯示該月的最後一個事件,而不是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

的醫生說,第一天的月份是1:http://developer.android.com/reference/java/util/Calendar.html#DAY_OF_MONTH ...但在你的情況下,似乎第一天將是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; 
+0

我該怎麼做,它不會取代? – input

+0

您需要提前創建數組並將值分配給索引。看我的例子。否則,您可以使用列表並添加。 – kosa

+0

我在前面創建了同樣的結果。只顯示最後一個事件:'summary_data = new Summary [e.size()]; \t \t對(INT J = 0;Ĵ input