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は 1 つのオブジェクトのみを返します。名前、タイトル、詳細を取得し、対応する要素のテキストをそれに設定したいと思います。どうすればこれを達成できますか? 名前、タイトル、詳細を個別のオブジェクトとして追加し、それらを個別のオブジェクトとして返し、それに応じてテキストを設定するにはどうすればよいですか? それらを配列に追加しますか? すべてのイベントの日付のようなもの:

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

返す方法は 2 つあります。1) 指定した配列として 2) 必要な変数とゲッター/セッターを使用して 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 に答える