Android在for循环中添加视图

嗨,我试图从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"}]}} 

回答:

你想不添加视图在一个循环什么的一个实例运行后,但是要创建一个适当的ListView和Sections。我建议您阅读本教程,其中包含示例代码。

Sectioning your ListView

以上是 Android在for循环中添加视图 的全部内容, 来源链接: utcz.com/qa/265092.html

回到顶部