2012-07-08 4 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

+0

하나의 날짜에 여러 이벤트를 추가하고 싶습니다. 내가 어떻게 그럴 수 있니? 'new event ("my-name1", "my-title1", "mydetails1")와 같은 이벤트는 새로운 이벤트 ("my-name2", "my-title2", "mydetails2")를 반환하지만 이것은 아닙니다 옳은 길. – input

+0

@ 입력,'List '을 반환하십시오. '리스트 events = new ArrayList (); events.add (새 이벤트 (....)); events.add (새 이벤트 (...)); return events; – hmjd

0

당신이 반환하는 방법은 두 가지, 1) (2)를 지정된 배열로) 필요한 변수 및 gettter/세터와 HolidayVO을 만듭니다. 사건을 바탕으로

:

case 1: 
    if (d == 1) { 
     //Create holiday object with values; 
    } else if (d == 10) { 
     //Create holiday object with values; 
    } 
    break; 
관련 문제