嗨,我已经创建了一个日期数组,我想为每个日期添加一个标题视图,然后我将运行另一个 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);
}
}