-1

我首先将 JSON 数组“日期”存储在 arratstring 中,并将我的日期与列表匹配,如果匹配则在此行之后显示该日期的“标题”名称 if (Vacation_Date.contains(mydate)) {

我只想在 textview 中打印与该日期标题名称匹配的值,请问我该怎么做

static ArrayList<Long> Vacation_ID = new ArrayList<Long>();
static ArrayList<String> Vacation_name = new ArrayList<String>();
static ArrayList<String> Vacation_Date = new ArrayList<String>();
         JSONObject json3 = new JSONObject(str2);
        status = json3.getString("status");
        if (status.equals("1")) {
        JSONArray school = json3.getJSONArray("data");
        for (int k = 0; k < school.length(); k++) {
            JSONObject jb = (JSONObject) school.getJSONObject(k);
            Vacation_ID.add((long) k);  
                    Vacation_Date.add(jb.getString("date"));
            }
        }

来自 .json 文件的强数组“日期” Vacation_Date。现在我将我的日期与 Vacation_Date 进行比较,并检查我的日期是否存在于 Vacation_Date 并在 textview 中显示其标题名称。

if (Vacation_Date.contains(mydate))     

//现在我做什么来显示比赛日期的标题名称

textview.settext ("title name of match date")'
 }

JSON

{"status":1,
"data":
[
    {"id":"1",
    "title":"abc",
    "date":"2013-09-29"},

    {"id":"2",
    "title":"abc1",
    "date":"2013-09-25"},

    {"id":"3",
    "title":"abc",
    "date":"2013-10-05"},

    {"id":"4",
    "title":"abc1",
    "date":"2013-09-27"}
]
}
4

1 回答 1

3

假设您想在日期等于 2013-10-05 时显示标题,因此创建一个方法来通过在jsonArraydate 属性上的条件中搜索来检索标题:

public String getTitleForDate(String searchDate) {
    for ( int i=0; i<array.length; i++) {
        JSONObject json = array.getJSONObject(i);
        String date = json.optString("date");
        if(date != null && searchDate.equals(date)) {
            return json.optString("title");
        }
    }
    return null;
}

然后TextView像这样显示标题:

if(Vacation_Date.contains(mydate)) {
String title = getTitleForDate(mydate); // i.e : in the case of mydate = "2013-10-05", it will return "abc" title
if(title != null)
    yourTextView.setText(title);
}
于 2013-09-20T11:20:09.517 回答