我的JSON响应以一个JSON数组开始,其中进一步包含三个数组。我想在卡片视图中显示这些阵列的内容。似乎在每个循环中,我总是在更改同一ViewHolder,因此结果始终是最后一个循环的最后一项。 int i
中的onBindViewHolder
表示要显示的项目的位置。
[
{
"firstTeam_VS_secondTeam": [{
"match_id": "248036",
"country_id": "169",
"country_name": "England",
"league_id": "70",
"league_name": "Capital One Cup",
"match_date": "2018-01-24",
"match_status": "FT",
"match_time": "21:00",
"match_hometeam_name": "Arsenal",
"match_hometeam_score": "2",
"match_awayteam_name": "Chelsea",
"match_awayteam_score": "1",
"match_hometeam_halftime_score": "1",
"match_awayteam_halftime_score": "1",
"match_live": "1"
},
.....
],"firstTeam_lastResults": [{
"match_id": "248036",
"country_id": "169",
"country_name": "England",
"league_id": "70",
"league_name": "Capital One Cup",
"match_date": "2018-01-24",
"match_status": "FT",
"match_time": "21:00",
"match_hometeam_name": "Arsenal",
"match_hometeam_score": "2",
"match_awayteam_name": "Chelsea",
"match_awayteam_score": "1",
"match_hometeam_halftime_score": "1",
"match_awayteam_halftime_score": "1",
"match_live": "1"
},
.....
], "secondTeam_lastResults": [{
"match_id": "248036",
"country_id": "169",
"country_name": "England",
"league_id": "70",
"league_name": "Capital One Cup",
"match_date": "2018-01-24",
"match_status": "FT",
"match_time": "21:00",
"match_hometeam_name": "Arsenal",
"match_hometeam_score": "2",
"match_awayteam_name": "Chelsea",
"match_awayteam_score": "1",
"match_hometeam_halftime_score": "1",
"match_awayteam_halftime_score": "1",
"match_live": "1"
},
.....
]
我的Onbindviewholder
类,我尝试过此代码,但是在我的所有名片视图中都显示相同的详细信息
@Override
public void onBindViewHolder(@NonNull footballmatchadapter.ViewHolder viewHolder, int i) {
List<FirstTeamVSSecondTeam> firstTeamVSSecondTeams;
List<FirstTeamLastResult> firstTeamLastResults;
List<SecondTeamLastResult> secondTeamLastResults;
firstTeamVSSecondTeams=post.getFirstTeamVSSecondTeam();
firstTeamLastResults=post.getFirstTeamLastResults();
secondTeamLastResults=post.getSecondTeamLastResults();
viewHolder.tvleague.setText(firstTeamVSSecondTeams.size());
viewHolder.tvhometeamscore.setText(firstTeamLastResults.size());
viewHolder.tvdate.setText(secondTeamLastResults.size());
if(post.getFirstTeamVSSecondTeam()!=null) {
firstTeamVSSecondTeams = post.getFirstTeamVSSecondTeam();
}
if(post.getFirstTeamLastResults()!=null) {
firstTeamLastResults = post.getFirstTeamLastResults();
}
if(post.getSecondTeamLastResults()!=null) {
secondTeamLastResults = post.getSecondTeamLastResults();
}
String leauge="",date="",team1="",team2="",team1score="",team2score="";
if(firstTeamVSSecondTeams!=null)
{
for(FirstTeamVSSecondTeam
firstTeamVSSecondTeam:firstTeamVSSecondTeams)
{
leauge=firstTeamVSSecondTeam.getLeagueName();
date="Match Date "+firstTeamVSSecondTeam.getMatchDate();
team1="Home Team "+firstTeamVSSecondTeam.getMatchHometeamName();
team2="Second Team
"+firstTeamVSSecondTeam.getMatchAwayteamName();
team1score=firstTeamVSSecondTeam.getMatchHometeamName()+" :
"+firstTeamVSSecondTeam.getMatchHometeamScore();
team2score=firstTeamVSSecondTeam.getMatchAwayteamName()+" :
"+firstTeamVSSecondTeam.getMatchAwayteamScore();
viewHolder.tvleague.setText(leauge);
viewHolder.tvdate.setText(date);
viewHolder.tvhometownteam.setText(team1);
viewHolder.tvawayteam.setText(team2);
viewHolder.tvhometeamscore.setText(team1score);
viewHolder.tvawayteamscore.setText(team2score);
}
}
if(firstTeamLastResults!=null)
{
for(FirstTeamLastResult firstTeamLastResult:firstTeamLastResults)
{
leauge=firstTeamLastResult.getLeagueName();
date="Match Date "+firstTeamLastResult.getMatchDate();
team1="Home Team "+firstTeamLastResult.getMatchHometeamName();
team2="Second Team "+firstTeamLastResult.getMatchAwayteamName();
team1score=firstTeamLastResult.getMatchHometeamName()+" :
"+firstTeamLastResult.getMatchHometeamScore();
team2score=firstTeamLastResult.getMatchAwayteamName()+" :
"+firstTeamLastResult.getMatchAwayteamScore();
viewHolder.tvleague.setText(leauge);
viewHolder.tvdate.setText(date);
viewHolder.tvhometownteam.setText(team1);
viewHolder.tvawayteam.setText(team2);
viewHolder.tvhometeamscore.setText(team1score);
viewHolder.tvawayteamscore.setText(team2score);
}
}
if(secondTeamLastResults!=null)
{
for(SecondTeamLastResult SecondTeamLastResult:secondTeamLastResults)
{
leauge=SecondTeamLastResult.getLeagueName();
date="Match Date "+SecondTeamLastResult.getMatchDate();
team1="Home Team "+SecondTeamLastResult.getMatchHometeamName();
team2="Second Team
"+SecondTeamLastResult.getMatchAwayteamName();
team1score=SecondTeamLastResult.getMatchHometeamName()+" :
"+SecondTeamLastResult.getMatchHometeamScore();
team2score=SecondTeamLastResult.getMatchAwayteamName()+" :
"+SecondTeamLastResult.getMatchAwayteamScore();
viewHolder.tvleague.setText(leauge);
viewHolder.tvdate.setText(date);
viewHolder.tvhometownteam.setText(team1);
viewHolder.tvawayteam.setText(team2);
viewHolder.tvhometeamscore.setText(team1score);
viewHolder.tvawayteamscore.setText(team2score);
}
}
}
这是我的ongetItemCount
函数,它返回所有三个数组的大小之和
@Override
public int getItemCount() {
int v1 = post.getFirstTeamVSSecondTeam() != null ?
post.getFirstTeamVSSecondTeam().size() : 0;
int v2 = post.getFirstTeamLastResults() != null ?
post.getFirstTeamLastResults().size() : 0;
int v3 = post.getSecondTeamLastResults() != null ?
post.getSecondTeamLastResults().size() : 0;
return v1 + v2 + v3;
}
答案 0 :(得分:1)
首先,似乎每个结果列表都不需要一个不同的类。您应该创建一个像这样的类:
public class MatchResults {
@SerializedName("match_id")
private long matchId;
@SerializedName("country_id")
private long countryId;
@SerializedName("country_name")
private String countryName;
...
}
您的适配器应如下所示:
public class FootballMatchAdapter extends RecyclerView.Adapter<FootballMatchAdapter.ViewHolder> {
private List<MatchResults> firstTeamVSSecondTeams;
private List<MatchResults> firstTeamLastResults;
private List<MatchResults> secondTeamLastResults;
public FootbalMatchAdapter() {
loadData();
}
private void loadData() {
firstTeamVSSecondTeams = post.getFirstTeamVSSecondTeam();
firstTeamLastResults = post.getFirstTeamLastResults();
secondTeamLastResults = post.getSecondTeamLastResults();
}
private int getFirstTeamVSSecondTeamsSize() {
return (firstTeamVSSecondTeams != null ?
firstTeamVSSecondTeams.size() : 0);
}
private int getFirstTeamLastResultsSize() {
return (firstTeamLastResults != null ?
firstTeamLastResults.size() : 0);
}
private int getSecondTeamLastResultsSize() {
return (secondTeamLastResults != null ?
secondTeamLastResults.size() : 0);
}
@Override
public int getItemCount() {
return getFirstTeamVSSecondTeamsSize()
+ getFirstTeamLastResultsSize()
+ getSecondTeamLastResultsSize();
}
private MatchResults getItem(int i) {
if (i < 0 || i > getItemCount())
return null;
if (i >= (getFirstTeamVSSecondTeamsSize() + getFirstTeamLastResultsSize())) {
int pos = i - (getFirstTeamVSSecondTeamsSize() + getFirstTeamLastResultsSize());
return secondTeamLastResults.get(pos);
}
else if (i >= getFirstTeamVSSecondTeamsSize()) {
int pos = i - getFirstTeamVSSecondTeamsSize();
return firstTeamLastResults.get(pos);
}
else {
return firstTeamVSSecondTeams.get(i);
}
}
@Override
public FootballMatchAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
...
}
@Override
public void onBindViewHolder(@NonNull footballmatchadapter.ViewHolder
viewHolder, int i) {
String leauge = "", date = "", team1 = "", team2 = "", team1score = "", team2score = "";
MatchResult result = getItem(i);
leauge = firstTeamVSSecondTeam.getLeagueName();
date = "Match Date " + firstTeamVSSecondTeam.getMatchDate();
team1 = "Home Team " + firstTeamVSSecondTeam.getMatchHometeamName();
team2 = "Second Team " + firstTeamVSSecondTeam.getMatchAwayteamName();
team1score = firstTeamVSSecondTeam.getMatchHometeamName() + " : "+firstTeamVSSecondTeam.getMatchHometeamScore();
team2score = firstTeamVSSecondTeam.getMatchAwayteamName() + " : "+firstTeamVSSecondTeam.getMatchAwayteamScore();
viewHolder.tvleague.setText(leauge);
viewHolder.tvdate.setText(date);
viewHolder.tvhometownteam.setText(team1);
viewHolder.tvawayteam.setText(team2);
viewHolder.tvhometeamscore.setText(team1score);
viewHolder.tvawayteamscore.setText(team2score);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
...
}
}
我没有运行此代码,因此可能有一些错误。