Android在for循环中添加视图

时间:2012-07-10 16:01:07

标签: android for-loop

您好我正在尝试从json Feed创建一个列表,该列表包含每个日期的标题,然后是下面的内容。然而,我已经按照日期顺序组织的json feed我创建了一个for循环,它获取了所有日期,然后检查重复项,这给了我一个来自feed的日期数组。然后,我想为每个日期添加一个标题视图到我的适配器,然后我假设我需要添加另一个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 feed的一个例子

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"}]}}

1 个答案:

答案 0 :(得分:0)

你想要的不是在循环中添加视图,而是使用Sections创建一个合适的ListView。我建议您阅读本教程,其中包含示例代码。

Sectioning your ListView