简体   繁体   中英

Return Multiple calendar events

The answer here by @hmjd helped me to set the text of multiple objects. But I have run into a problem now. A date can have multiple events and I would like to show all the events and their details on the same event details page. How can I do this?

Code:

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

To store the events returned by eventDetails, you would do something like this:

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

Then to access the Events stored in 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

If you want to make this dynamic instead of explicitly saying e.get(0) , you would loop over the size of the ArrayList as follows:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM