我在如何向回收器视图显示json数据方面遇到了问题。这是我的json数据,我想在一个回收器视图中显示“player”和“coach”对象值。我已经使用了一些其他的json结构,但是这个对我来说有点复杂,所以我不知道该怎么做。
{"success":1,"result":[{"team_key":"2616","team_name":"Chelsea","team_logo":"https:\/\/allsportsapi.com\/logo\/2616_chelsea.png","players":[{"player_key":3440253915,"player_name":"Arrizabalaga Kepa","player_number":"1","player_country":"Spain","player_type":"Goalkeepers","player_age":"26","player_match_played":"3","player_goals":"0","player_yellow_cards":"0","player_red_cards":"0"},{"player_key":2059460065,"player_name":"Caballero Wilfredo","player_number":"13","player_country":"Argentina","player_type":"Goalkeepers","player_age":"39","player_match_played":"1","player_goals":"0","player_yellow_cards":"0","player_red_cards":"0"},{"player_key":2488764308,"player_name":"Mendy Edouard","player_number":"16","player_country":"Senegal","player_type":"Goalkeepers","player_age":"28","player_match_played":"12","player_goals":"0","player_yellow_cards":"0","player_red_cards":"0"},{"player_key":3214711283,"player_name":"Alonso Marcos","player_number":"3","player_country":"Spain","player_type":"Defenders","player_age":"30","player_match_played":"3","player_goals":"0","player_yellow_cards":"1","player_red_cards":"0"},{"player_key":749545136,"player_name":"Azpilicueta Cesar","player_number":"28","player_country":"Spain","player_type":"Defenders","player_age":"31","player_match_played":"7","player_goals":"0","player_yellow_cards":"2","player_red_cards":"0"},{"player_key":2242127097,"player_name":"Chilwell Benjamin","player_number":"21","player_country":"England","player_type":"Defenders","player_age":"24","player_match_played":"13","player_goals":"2","player_yellow_cards":"1","player_red_cards":"0"},{"player_key":2456000699,"player_name":"Christensen Andreas","player_number":"4","player_country":"Denmark","player_type":"Defenders","player_age":"24","player_match_played":"5","player_goals":"0","player_yellow_cards":"1","player_red_cards":"1"},{"player_key":2294955514,"player_name":"Emerson","player_number":"33","player_country":"Italy","player_type":"Defenders","player_age":"26","player_match_played":"2","player_goals":"0","player_yellow_cards":"0","player_red_cards":"0"},{"player_key":3619559992,"player_name":"James Reece","player_number":"24","player_country":"England","player_type":"Defenders","player_age":"21","player_match_played":"13","player_goals":"1","player_yellow_cards":"3","player_red_cards":"0"},{"player_key":2991724348,"player_name":"Rudiger Antonio","player_number":"2","player_country":"Germany","player_type":"Defenders","player_age":"27","player_match_played":"2","player_goals":"0","player_yellow_cards":"0","player_red_cards":"0"},{"player_key":3356512369,"player_name":"Silva Thiago","player_number":"6","player_country":"Brazil","player_type":"Defenders","player_age":"36","player_match_played":"11","player_goals":"2","player_yellow_cards":"2","player_red_cards":"0"},{"player_key":452013637,"player_name":"Tomori Fikayo","player_number":"14","player_country":"England","player_type":"Defenders","player_age":"23","player_match_played":"1","player_goals":"0","player_yellow_cards":"0","player_red_cards":"0"},{"player_key":1529403595,"player_name":"Zouma Kurt","player_number":"15","player_country":"France","player_type":"Defenders","player_age":"26","player_match_played":"14","player_goals":"4","player_yellow_cards":"1","player_red_cards":"0"},{"player_key":3998708501,"player_name":"Gilmour Billy","player_number":"23","player_country":"Scotland","player_type":"Midfielders","player_age":"19","player_match_played":"1","player_goals":"0","player_yellow_cards":"0","player_red_cards":"0"},{"player_key":3868193151,"player_name":"Jorginho","player_number":"5","player_country":"Italy","player_type":"Midfielders","player_age":"29","player_match_played":"10","player_goals":"3","player_yellow_cards":"0","player_red_cards":"0"},{"player_key":717905378,"player_name":"Kante N'Golo","player_number":"7","player_country":"France","player_type":"Midfielders","player_age":"29","player_match_played":"16","player_goals":"0","player_yellow_cards":"4","player_red_cards":"0"},{"player_key":3915727687,"player_name":"Kovacic Mateo","player_number":"17","player_country":"Croatia","player_type":"Midfielders","player_age":"26","player_match_played":"11","player_goals":"0","player_yellow_cards":"0","player_red_cards":"0"},{"player_key":1466702728,"player_name":"Mount Mason","player_number":"19","player_country":"England","player_type":"Midfielders","player_age":"21","player_match_played":"15","player_goals":"1","player_yellow_cards":"2","player_red_cards":"0"},{"player_key":1747265972,"player_name":"Pulisic Christian","player_number":"10","player_country":"USA","player_type":"Midfielders","player_age":"22","player_match_played":"9","player_goals":"1","player_yellow_cards":"0","player_red_cards":"0"},{"player_key":3026064276,"player_name":"Ziyech Hakim","player_number":"22","player_country":"Morocco","player_type":"Midfielders","player_age":"27","player_match_played":"7","player_goals":"1","player_yellow_cards":"1","player_red_cards":"0"},{"player_key":2712092737,"player_name":"Abraham Tammy","player_number":"9","player_country":"England","player_type":"Forwards","player_age":"23","player_match_played":"14","player_goals":"6","player_yellow_cards":"0","player_red_cards":"0"},{"player_key":3085557745,"player_name":"Giroud Olivier","player_number":"18","player_country":"France","player_type":"Forwards","player_age":"34","player_match_played":"9","player_goals":"3","player_yellow_cards":"1","player_red_cards":"0"},{"player_key":1087701594,"player_name":"Havertz Kai","player_number":"29","player_country":"Germany","player_type":"Forwards","player_age":"21","player_match_played":"14","player_goals":"1","player_yellow_cards":"1","player_red_cards":"0"},{"player_key":2773006276,"player_name":"Hudson-Odoi Callum","player_number":"20","player_country":"England","player_type":"Forwards","player_age":"20","player_match_played":"7","player_goals":"1","player_yellow_cards":"0","player_red_cards":"0"},{"player_key":640779808,"player_name":"Werner Timo","player_number":"11","player_country":"Germany","player_type":"Forwards","player_age":"24","player_match_played":"16","player_goals":"4","player_yellow_cards":"0","player_red_cards":"0"}],"coaches":[{"coach_name":"Lampard Frank","coach_country":"England","coach_age":"42"}]}]}
Mainactivity
enter code herepackage com.example.teams.activity;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.example.teams.R;
import com.example.teams.adapter.TeamsAdapter;
import com.example.teams.model.Player;
import com.example.teams.model.Result;
import com.example.teams.model.TeamsMain;
import com.example.teams.rest.Api;
import com.example.teams.rest.ApiClient;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
private final static String TAG=MainActivity.class.getSimpleName();
RecyclerView recyclerView;
private static final String API_key ="c3d5fc8869402cf4bb4b5e162b3c3aea30e4bb53f90607d3e621e9f6a250e30e";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=findViewById(R.id.recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Api api= ApiClient.getClient().create(Api.class);
Call<TeamsMain> call=api.getResult(API_key);
call.enqueue(new Callback<TeamsMain>() {
@Override
public void onResponse(Call<TeamsMain> call, Response<TeamsMain> response) {
if(!response.isSuccessful()){
Toast.makeText(getApplicationContext(),"error"+response.errorBody(),Toast.LENGTH_SHORT).show();
return;
}
List<Result> resultList=response.body().getResult();
recyclerView.setAdapter(new TeamsAdapter(resultList,R.layout.teams,getApplicationContext()));
Log.d(TAG,"DATA"+resultList.size());
Log.e("MainActivity","response"+response.body().toString());
Log.d(TAG, "message received men");
}
@Override
public void onFailure(Call<TeamsMain> call, Throwable t) {
Log.e("problem",t.toString());
}
});
}
}
TEAMSADAPTER
enter code herepackage com.example.teams.model;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;
public class TeamsMain {
@SerializedName("success")
private Integer success;
@SerializedName("result")
private List<Result> result ;
public TeamsMain(Integer success, List<Result> result) {
this.success = success;
this.result = result;
}
public Integer getSuccess() {
return success;
}
public void setSuccess(Integer success) {
this.success = success;
}
public List<Result> getResult() {
return result;
}
public void setResult(List<Result> result) {
this.result = result;
}
}
MODEL CLASSES
1. RESULTS
package com.example.teams.model;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;
public class Result {
@SerializedName("team_key")
private String teamKey;
@SerializedName("team_name")
private String teamName;
@SerializedName("team_logo")
private String teamLogo;
@SerializedName("players")
private Player players;
@SerializedName("coaches")
private List<Coach>Coaches=new ArrayList<Coach>();
public Result(String teamKey, String teamName, String teamLogo, Player players, List<Coach> coaches) {
this.teamKey = teamKey;
this.teamName = teamName;
this.teamLogo = teamLogo;
this.players = players;
this.Coaches = coaches;
}
public String getTeamKey() {
return teamKey;
}
public String getTeamName() {
return teamName;
}
public String getTeamLogo() {
return teamLogo;
}
public Player getPlayers() {
return players;
}
public List<Coach> getCoaches() {
return Coaches;
}
public void setTeamKey(String teamKey) {
this.teamKey = teamKey;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public void setTeamLogo(String teamLogo) {
this.teamLogo = teamLogo;
}
public void setPlayers(Player players) {
this.players = players;
}
public void setCoaches(List<Coach> coaches) {
Coaches = coaches;
}
}
2. COACH
package com.example.teams.model;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Coach {
@SerializedName("coach_name")
private String coachName;
@SerializedName("coach_country")
private String coachCountry;
@SerializedName("coach_age")
private String coachAge;
public Coach(String coachName, String coachCountry, String coachAge) {
this.coachName = coachName;
this.coachCountry = coachCountry;
this.coachAge = coachAge;
}
public String getCoachName() {
return coachName;
}
public void setCoachName(String coachName) {
this.coachName = coachName;
}
public String getCoachCountry() {
return coachCountry;
}
public void setCoachCountry(String coachCountry) {
this.coachCountry = coachCountry;
}
public String getCoachAge() {
return coachAge;
}
public void setCoachAge(String coachAge) {
this.coachAge = coachAge;
}
}
3. PLAYER
package com.example.teams.model;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Player {
@SerializedName("player_key")
private long playerKey;
@SerializedName("player_name")
private String playerName;
@SerializedName("player_number")
private String playerNumber;
@SerializedName("player_country")
private String playerCountry;
@SerializedName("player_type")
private String playerType;
@SerializedName("player_age")
private String playerAge;
@SerializedName("player_match_played")
private String playerMatchPlayed;
@SerializedName("player_goals")
private String playerGoals;
@SerializedName("player_yellow_cards")
private String playerYellowCards;
@SerializedName("player_Red_cards")
private String playerRedCards;
public Player(Integer playerKey, String playerName, String
playerNumber, String playerCountry, String playerType, String
playerAge, String playerMatchPlayed, String playerGoals, String playerYellowCards, String playerRedCards) {
this.playerKey = playerKey;
this.playerName = playerName;
this.playerNumber = playerNumber;
this.playerCountry = playerCountry;
this.playerType = playerType;
this.playerAge = playerAge;
this.playerMatchPlayed = playerMatchPlayed;
this.playerGoals = playerGoals;
this.playerYellowCards = playerYellowCards;
this.playerRedCards = playerRedCards;
}
public long getPlayerKey() {
return playerKey;
}
public String getPlayerName() {
return playerName;
}
public String getPlayerNumber() {
return playerNumber;
}
public String getPlayerCountry() {
return playerCountry;
}
public String getPlayerType() {
return playerType;
}
public String getPlayerAge() {
return playerAge;
}
public String getPlayerMatchPlayed() {
return playerMatchPlayed;
}
public String getPlayerGoals() {
return playerGoals;
}
public String getPlayerYellowCards() {
return playerYellowCards;
}
public String getPlayerRedCards() {
return playerRedCards;
}
public void setPlayerKey(Integer playerKey) {
this.playerKey = playerKey;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public void setPlayerNumber(String playerNumber) {
this.playerNumber = playerNumber;
}
public void setPlayerCountry(String playerCountry) {
this.playerCountry = playerCountry;
}
public void setPlayerType(String playerType) {
this.playerType = playerType;
}
public void setPlayerAge(String playerAge) {
this.playerAge = playerAge;
}
public void setPlayerMatchPlayed(String playerMatchPlayed) {
this.playerMatchPlayed = playerMatchPlayed;
}
public void setPlayerGoals(String playerGoals) {
this.playerGoals = playerGoals;
}
public void setPlayerYellowCards(String playerYellowCards) {
this.playerYellowCards = playerYellowCards;
}
public void setPlayerRedCards(String playerRedCards) {
this.playerRedCards = playerRedCards;
}
}
4. TEAMSMAIN
package com.example.teams.model;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;
public class TeamsMain {
@SerializedName("success")
private Integer success;
@SerializedName("result")
private List<Result> result ;
public TeamsMain(Integer success, List<Result> result) {
this.success = success;
this.result = result;
}
public Integer getSuccess() {
return success;
}
public void setSuccess(Integer success) {
this.success = success;
}
public List<Result> getResult() {
return result;
}
public void setResult(List<Result> result) {
this.result = result;
}
}
暂无答案!
目前还没有任何答案,快来回答吧!