0

@hmjd这里的答案帮助我设置了多个对象的文本。但是我现在遇到了一个问题。一个日期可以有多个事件,我想在同一个事件详细信息页面上显示所有事件及其详细信息。我怎样才能做到这一点?

代码:

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

1 回答 1

1

要存储 eventDetails 返回的事件,您可以执行以下操作:

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

然后访问存储在 ArrayList e 中的事件:

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

如果你想让这个动态而不是明确地说e.get(0),你可以循环遍历 ArrayList 的大小,如下所示:

for (int i = 0; i < e.size(); i++)
{
    Event ev = e.get(i);
    ev.doSomething();
    ev.doSomethingElse();
}
于 2012-07-11T14:52:07.570 回答