Android以编程方式在for循环中添加视图

时间:2012-07-05 09:25:33

标签: android for-loop

您好我创建了一个日期数组,我想为每个日期添加一个标题视图,然后我会运行另一个for循环,在每个标题下面添加其他视图。目前我只是想添加标题视图,但目前屏幕上没有显示任何内容。所以我的问题是如何在for循环中以编程方式添加视图?

继承我的代码

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


}     


}

1 个答案:

答案 0 :(得分:0)

我认为这就是你要搜索的内容

http://code.google.com/p/android-amazing-listview/

带有节标题的ListView和下一页的自动加载(即最后一项上的“正在加载...”)