2012-07-10 114 views
0

嗨,我試圖從json提要創建一個列表,每個日期有一個標題,然後在下面的內容。然而,我沒有按照日期順序來組織json feed,我已經創建了一個for循環來獲取所有日期,然後檢查重複項,這給了我一個來自feed的日期數組。然後我想爲每個日期的適配器添加一個標題視圖,我想我需要添加另一個循環來獲取每個日期下的內容?問題是目前我的標題查看Android在for循環中添加視圖

所以我的問題是我如何創建一個基於for循環添加標題視圖的列表,然後在每個標題下添加其他視圖?

這裏是我的功能,我是一個的AsyncTask

public void FillData() throws JSONException{  

     ListView list = getListView(); 
     list.scrollTo(0, 0); 
     list.setVerticalFadingEdgeEnabled(false); 

      list.setTextFilterEnabled(true); 

      LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View header = inflater.inflate(R.layout.homeheader, list, false); 

     fixturesView = LayoutInflater.from(getBaseContext()).inflate(R.layout.fixturescell, 
       null); 


     //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{ 

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

       if(matchDatesArray.length() != 0){ 

        int lm = t - 1; 
        JSONObject lastDateDict = fixturesArray.getJSONObject(lm); 
        String lastMatchDate = lastDateDict.getString("matchdate"); 

        Log.v("MyFix", "lastMatchDate " + lastMatchDate); 

        if(matchDate.equals(lastMatchDate)){ 
         Log.v("MyFix", "2 are the same");       
        } else { 
         Log.v("MyFix", "add new matchdate to array"); 
         matchDatesArray.put(matchDate); 
        } 

       } else { 
        Log.v("MyFix", "add new matchdate to array (first time only)");      
        matchDatesArray.put(matchDate);  
       } 
      } 


      Log.v("MyFix", "matchDatesArray = " + matchDatesArray); 

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

     DateHeader = LayoutInflater.from(getBaseContext()).inflate(R.layout.redcell, 
       null); 

     adapter = new MergeAdapter(); 

     for(int t = 0; t < matchDatesArray.length(); t++){  

      JSONObject mdheaderdict = matchDatesArray.getJSONObject(t); 
      String matchheader = mdheaderdict.getString("matchdate"); 

       TextView matchdayheader = (TextView) DateHeader.findViewById(R.id.redheadertext); 
       matchdayheader.setText(matchheader); 
       adapter.addView(DateHeader); 
     }    
    }  
     setListAdapter(adapter); 

} 

繼承人的JSON提要

fixturesdata{"code":200,"error":null,"data":{"fixtures":[{"kickoff":"15:00:00","matchdate":"2012-07-14","homescore":null,"awayscore":null,"attendance":null,"homepens":null,"awaypens":null,"division_id":"5059","division":"Testing 1","comp":"LGE","location":null,"fixture_note":null,"hometeam_id":"64930","hometeam":"Team 1","awayteam_id":"64933","awayteam":"Team 4"},{"kickoff":"15:00:00","matchdate":"2012-07-14","homescore":null,"awayscore":null,"attendance":null,"homepens":null,"awaypens":null,"division_id":"5059","division":"Testing 1","comp":"LGE","location":null,"fixture_note":null,"hometeam_id":"64935","hometeam":"Team 6","awayteam_id":"64937","awayteam":"Team 8"},{"kickoff":"15:00:00","matchdate":"2012-07-28","homescore":null,"awayscore":null,"attendance":null,"homepens":null,"awaypens":null,"division_id":"5059","division":"Testing 1","comp":"LGE","location":null,"fixture_note":null,"hometeam_id":"64930","hometeam":"Team 1","awayteam_id":"64931","awayteam":"Team 2"},{"kickoff":"15:00:00","matchdate":"2012-07-28","homescore":null,"awayscore":null,"attendance":null,"homepens":null,"awaypens":null,"division_id":"5059","division":"Testing 1","comp":"LGE","location":null,"fixture_note":null,"hometeam_id":"64930","hometeam":"Team 1","awayteam_id":"64931","awayteam":"Team 2"}]}} 
+0

你的問題到底是什麼? – kcoppock 2012-07-10 16:11:32

+0

對不起,所以我的問題是如何創建一個基於for循環添加標題視圖的列表,然後在每個標題下添加其他視圖? – 2012-07-10 16:18:32

+0

也許一個ExpandableListView會工作? – Joel 2012-07-10 16:21:48

回答

0

你想不添加視圖在一個循環什麼的一個實例運行後,但是要創建一個適當的ListView和Sections。我建議您閱讀本教程,其中包含示例代碼。

Sectioning your ListView