0
@hmjd的答案here幫助我設置多個對象的文本。但我現在遇到了一個問題。日期可以有多個事件,我想在同一事件詳細信息頁面上顯示所有事件及其詳細信息。我怎樣才能做到這一點?返回多個日曆事件
代碼:
public class Event
{
public final String name;
public final String title;
public final String details;
public Event(final String a_name,
final String a_title,
final String a_details)
{
name = a_name;
title = a_title;
details = a_details;
}
};
final Event e = eventDetails(1, 4);
name.setText(e.name);
title.setText(e.title);
details.setText(e.details);
//event details
public Event eventDetails(int m, int d) {
switch (m) {
case 1:
if (d == 10) {
return new Event("event1", "my-title1", "mydetails1");
}
if (d == 28) {
return new Event("event2", "my-title1", "mydetails1");
return new Event("event3", "my-title2", "mydetails2"); //add another event on this date; obviously this is not the right way.
}
break;
}
return new Event("default_my-name2", "default_my-title2", "default_mydetails2");
}
添加事件到ArrayList,然後相應地處理該ArrayList裏面每個事件? –
adchilds
2012-07-09 19:13:06
@adchilds,我做到了。 //事件詳細信息 public ArrayList eventDetails(int m,int d){ \t ArrayList events = new ArrayList (); 開關(M){ \t \t \t如果(d == 15){ \t \t \t \t events.add(新事件( 「事件1」, 「EVENTTITLE」, 「event_Details」)); (新事件(「event2」,「eventtitle」,「event_Details」)); \t \t \t \t返回事件; \t \t \t} break;我在調用eventDetails函數的時候拋出一個錯誤:'final Event e = eventDetails(m,d);'錯誤:'類型不匹配:不能從ArrayList to HijriEvents.Event' –
input
2012-07-09 19:50:49
錯誤告訴你到底發生了什麼問題。您試圖將eventDetails的返回類型(它返回Event類型的ArrayList)存儲到正常的Event中。您不能將ArrayList存儲到事件中。另外,你的switch語句現在不做任何事情(在剛剛提供的代碼中,原始文章中沒有問題)? 最終事件e 應該是ArrayList類型。然後你可以從該ArrayList中提取每個事件。 – adchilds 2012-07-10 14:15:56