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

Stringholiday只返回一个对象。我想获取名称、标题和详细信息并将相应元素的文本设置为它。我怎样才能做到这一点?如何将名称、标题和详细信息添加为单独的对象并将它们作为单独的对象返回以相应地设置文本?我是否将它们添加到数组中?每个活动日期都类似:

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

如果是这样,我如何返回数组来设置文本?

4

2 回答 2

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);
于 2012-07-08T21:03:25.443 回答
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; 
于 2012-07-08T21:03:36.367 回答