2012-07-08 33 views
0

我有一個日曆,如果用戶選擇日期並將其帶到事件詳細信息頁面。在事件詳細信息頁面上,數據以表格格式顯示。到現在爲止還挺好。問題是如何在eventDetails函數中設置事件的細節並返回它們來設置文本。返回數組中的日曆事件詳細信息

代碼:

//get the data and split it 
    String[] dateAr = date_string.split("-|\\||\\(|\\)|\\s+"); 
    m = Integer.parseInt(dateAr[6]); 
    d = Integer.parseInt(dateAr[3]); 
    y = Integer.parseInt(dateAr[8]); 

    name = (TextView) this.findViewById(R.id.name); 
    title = (TextView) this.findViewById(R.id.title); 
    details = (TextView) this.findViewById(R.id.details); 


    name.setText(name_details); //should get the info from the eventDetails method 
    title.setText(title_details); //should get the info from the eventDetails method 
    details.setText(event_details); //should get the info from the eventDetails method 

    //event details 
    public String eventDetails(int m, int d) { 
    String holiday = ""; 
    switch (m) { 
     case 1: 
      if (d == 1) { 
       holiday = "Some event"; 
      } else if (d == 10) { 
       holiday = "Some event"; 
      } 
      break; 
     case 3: 
      if ((d == 11) || (d == 12)) { 
       holiday = "Some event"; 
      } 
      break; 
     case 7: 
      if ((d == 1) && (d== 7)) { 
       holiday = "Some event"; 
      } 
      break; 
    } 

    return holiday; 
} 

絃樂holiday只返回一個對象。我想獲取名稱,標題和詳細信息,併爲其設置相應的元素文本。我怎樣才能做到這一點?我如何將名稱,標題和細節添加爲單獨的對象,並將它們作爲單獨的對象返回以相應地設置文本?我將它們添加到數組?像每一個事件日期:

String holiday[] = {"name of the event", "title of the event", "details of the event"}; 

如果是這樣,我怎麼返回數組來設置文本?

回答

1

創建一個包含這些事件詳細信息的class並返回它的一個實例。例如:

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

public Event eventDetails(int m, int d) { 
    if (some-condition) 
     return new Event("my-name1", "my-title1", "mydetails1"); 
    else 
     return new Event("my-name2", "my-title2", "mydetails2"); 
} 

final Event e = eventDetails(1, 4); 
name.setText(e.name); 
title.setText(e.title); 
details.setText(e.details); 
+0

非常感謝您! – input 2012-07-08 21:54:27

+0

我想添加多個事件到一個日期?我會怎麼做?類似於'返回新事件(「my-name1」,「my-title1」,「mydetails1」)返回新事件(「my-name2」,「my-title2」,「mydetails2」);'但這不是正確的方式。 – input 2012-07-11 13:30:12

+0

@input,返回'List '。 'List events = new ArrayList (); events.add(new Event(....)); events.add(new Event(...));返回事件;' – hmjd 2012-07-11 13:31:53

0

有兩種方法可以返回,1)陣列指定2)所需的變量和gettter/setter方法創建HolidayVO。

基於案例:

case 1: 
    if (d == 1) { 
     //Create holiday object with values; 
    } else if (d == 10) { 
     //Create holiday object with values; 
    } 
    break; 
相關問題