0

嗨,我正在创建一个列表,其中包含日期标题和下面的内容。我有一个 JSON 提要,其中每个内部包含灯具是一个数组,其中包含每个灯具的数据我需要的一个字符串是 matchdate 来创建我的标题但是如果我只是通过它运行它将创建同一比赛日的多个实例所以 id例如,有 3 个具有相同日期的标题。我如何提取该信息并创建另一个数组来说明该日期是否已经存在,然后通过下一个等。我知道这是一个非常具体的问题,但如果有人至少可以指出我正确的方向。提前致谢。

这是我的饲料

 fixturesArray = [{"awayteam":"Team 1","comp":"LGE","location":null,"attendance":null,"awayscore":null,"division":"Testing 1","homescore":null,"fixture_note":null,"kickoff":"15:30:00","awayteam_id":"64930","matchdate":"2012-07-07","awaypens":null,"homepens":null,"division_id":"5059","hometeam":"Team 3","hometeam_id":"64932"},{"awayteam":"Team 2","comp":"LGE","location":null,"attendance":null,"awayscore":null,"division":"Testing 1","homescore":null,"fixture_note":null,"kickoff":"15:00:00","awayteam_id":"64931","matchdate":"2012-07-07","awaypens":null,"homepens":null,"division_id":"5059","hometeam":"Team 4","hometeam_id":"64933"},{"awayteam":"Team 4","comp":"LGE","location":null,"attendance":null,"awayscore":null,"division":"Testing 1","homescore":null,"fixture_note":null,"kickoff":"15:00:00","awayteam_id":"64933","matchdate":"2012-07-14","awaypens":null,"homepens":null,"division_id":"5059","hometeam":"Team 1","hometeam_id":"64930"}]

这是我到目前为止所尝试的

Log.v("MyFix", "fixturesArray = " + fixturesArray);
       if(fixturesArray.length() < 1){

             TextView emptytext = (TextView) fixturesView.findViewById(R.id.TextView02);
             emptytext.setText("No Upcoming Fixtures Available");

       }else{
        try{   


            JSONArray datesArray = null;
            fixturesInfo = null;
            String matchDateTemp = "";

            for(int t = 0; t < fixturesArray.length(); t++){
               JSONObject matchDateDict = fixturesArray.getJSONObject(t);
               String matchDate = matchDateDict.getString("matchdate");
               JSONArray matchdates = matchdates.put(matchDate);

               Log.v("MyFix", "matchdate = " + matchDate);

               tempArray.put(t, fixturesArray);
               fixturesInfo.put(matchDate, tempArray);

            }

            Log.v("MyFix", "fixturesInfo = " + fixturesInfo);
            Log.v("MyFix", "tempArray = " + tempArray);


        }catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
4

1 回答 1

0

只需获取一条数据,然后在添加之前遍历新的 arraylist 检查是否存在重复项。

int matchCount=0;                                           // set count of matches to 0
String matchDate = matchDateDict.getString("matchdate");    // get the data to compare
for (int i = 0; i < uniqueDateArrayList.size(); i++){       // set loop to iterate through unique date arraylist
    if (matchDate.equals(uniqueDateArray[i])){              // compare the date to the date stored at position i
        matchCount++;                                       // if it matches increase the match count
    }
}

if (matchCount == 0) {
    uniqueDateArrayList.add(matchDate)                       // if there is no matching date, add it to the unique date arraylist
}

这应该会为您提供一个包含数据中所有唯一日期的数组列表。

于 2012-07-04T16:02:28.387 回答