我有一个带有TabLayout + ViewPager的项目,可以滚动浏览两个不同的片段(一个显示当前发生的数据,另一个显示所有数据)。
在onCreateView的两个中,我调用相同的ASyncTask类,唯一的区别是params更改。
FetchItems asyncTask = new FetchItems(getContext(), this);
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"someParams");
然后在此处处理JSON结果:
try {
LinkedList<LeagueItem> leagues = new LinkedList<>();
LeagueItem newLeague;
//For every league in the array
for (int i = 0; i < jsonBody.length(); i++) {
newLeague = new LeagueItem();
//Hold the league games
JSONObject jsonLeagueInfo = jsonBody.getJSONObject(i);
newLeague.setLeagueName(jsonLeagueInfo.getString("name"));
JSONArray leagueMatches = jsonLeagueInfo.getJSONArray("matches");
//For every match in that league
for(int j = 0;j<leagueMatches.length();j++){
JSONObject matchInformation = leagueMatches.getJSONObject(j);
JSONArray teamsArray = matchInformation.getJSONArray("teams");
MatchItem currentMatch = new MatchItem();
//For both teams in the match
for(int k=0;k<2;k++){
JSONObject teamInfo = teamsArray.getJSONObject(k);
String name = teamInfo.getString("name");
String logoUrl = teamInfo.getString("logo");
JSONObject scores = teamInfo.getJSONObject("results");
String runningScore = scores.getString("runningscore");
TeamItem currentTeam = new TeamItem(shortName, logoUrl, Integer.parseInt(runningScore), homeNum);
currentMatch.addTeam(currentTeam);
}
newLeague.addMatch(currentMatch);
}
leagues.add(newLeague);
}
return leagues;
}
我发现返回的两个对象都有交叉数据,不应该存在。两个父对象都是正确的,因为它们添加了正确数量的联赛项目,但是每个联盟都包含了我正在迭代的所有数据。我错过了一些巨大的东西吗?我认为通过调用executeOnExecuter,我将获得两个完全独立的线程和不同的对象。
感谢。