2012-07-09 4 views
0

@hmjd의 답변 here을 사용하면 여러 개체의 텍스트를 설정할 수있었습니다. 하지만 지금 문제가 생겼어. 날짜에는 여러 개의 이벤트가있을 수 있으며 모든 이벤트와 세부 정보를 동일한 이벤트 세부 정보 페이지에 표시하고 싶습니다. 어떻게해야합니까?여러 캘린더 이벤트를 반환

코드 : eventDetails에 의해 반환 된 이벤트를 저장하려면

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"); 
} 
+0

는 ArrayList에 에 이벤트를 추가하고 그에 따라 ArrayList에 내부의 각 이벤트를 처리? – adchilds

+0

@adchilds, 내가 그랬어. '// 이벤트 정보 공개 ArrayList를 eventDetails (INT의 m, INT의 d) { \t ArrayList를 이벤트 = 새로운 ArrayList를 (); 스위치 (m) { \t \t \t 경우 (d의 == 15) { \t \t \t \t events.add (새로운 이벤트 ("합니다 Event1", "EVENTTITLE", "event_Details")); \t \t \t \t events.add (새 이벤트 ("event2", "eventtitle", "event_Details"))); \t \t \t \t 반환 이벤트; \t \t \t} 브레이크; \t \t} }'event 이벤트가 = eventDetails (m, d);'오류 :'타입 불일치 : ArrayList to HijriEvents.Event' – input

+0

오류는 무엇이 잘못되었는지를 알려줍니다. EventDetails의 반환 유형 (이벤트 유형의 ArrayList를 반환)을 일반 이벤트에 저장하려고합니다. ArrayList를 Event에 저장할 수 없습니다. 또한 switch 문은 현재 제공하지 않는 코드를 제공합니다 (원래 코드에서는 괜찮습니다). 최종 이벤트 e 은 ArrayList 유형이어야합니다. 그런 다음 해당 ArrayList에서 각 이벤트를 추출 할 수 있습니다. – adchilds

답변

1

, 당신은 같은 것을 할 것입니다 :

Event one = e.get(0); // First Event in the ArrayList 
Event two = e.get(1); // Second Event in the ArrayList 
... 
Event n = e.get(n); // nth Event in the ArrayList 
:

다음
ArrayList<Event> e = new ArrayList<Event>(); 
e.add(eventDetails(1, 4)); // This adds one event to the ArrayList 

는 이벤트 ArrayList를 전자에 저장에 액세스 할 수

명시 적으로 e.get(0)이라고 말하기보다는 동적으로 만들려면 다음과 같이 ArrayList의 크기를 통해 루프는 다음과 같습니다

for (int i = 0; i < e.size(); i++) 
{ 
    Event ev = e.get(i); 
    ev.doSomething(); 
    ev.doSomethingElse(); 
} 
+0

고맙습니다. – input