Android从Array获取字符串并创建另一个数组

时间:2012-07-04 15:37:37

标签: android

您好我正在创建一个列表,其中包含日期标题,然后是内容。我有一个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();
            }

1 个答案:

答案 0 :(得分: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
}

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