2012-07-04 187 views
0

嗨創建一個列表,其中包含日期的標題,然後下面的內容。我有一個JSON提要,其中包含夾具裏面每個是一個數組包含每個fixture的數據我需要的一個字符串是matchdate來創建我的頭然而如果我只是通過它運行,它會創建同一個匹配日的多個實例,所以id例如,有3個標題與日期相同。我如何提取該信息並創建另一個數組,說明此日期是否已經存在,然後再通過下一個數據庫等等。我知道這是一個非常具體的問題,但是如果有人能夠至少指出我正確的方向。提前致謝。Android從一個數組中獲取字符串並創建另一個數組

我的繼承人飼料

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

回答

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 
} 

這應該給你一個包含數據中所有唯一日期的數組列表。

相關問題