已经有2天我正在研究这个问题并且我搜索的时候我没有找到关于这个主题的答案,所以我在这里尝试: 在我的writeToParcel方法中,我发送自定义列表,这就是我使用writeTypedList方法的原因。问题是,当我使用它时,它返回一个空列表。我给你我的代码:
public class Team implements Parcelable {
private List<Player> players;
private String team_name;
private String sport_name;
private String team_leader;
private String training_day;
private String training_hour;
public Team(){
players = new ArrayList<Player>() ;
addPlayer(new Player(team_leader));
}
protected Team(String team_name, String sport_name, String team_leader, String training_day, String training_hour) {
players = new ArrayList<Player>();
this.team_name = team_name;
this.sport_name = sport_name;
this.team_leader = team_leader;
this.training_day = training_day;
this.training_hour = training_hour;
addPlayer(new Player(team_leader));
}
public Team(Parcel in) {
this();
team_name = in.readString();
sport_name = in.readString();
team_leader= in.readString();
training_day = in.readString();
training_hour = in.readString();
System.out.println("in Team Parcel" +in.toString());
//ArrayList <Player> tempPlayers = new ArrayList<Player>();
System.out.println("in Team 2" +players);
in.readTypedList(players,Player.CREATOR);
//players = in.createTypedArrayList(Player.CREATOR);
}
public List<Player> getTeam(){
if(players == null) players = new ArrayList<>();
return players;
}
public void addPlayer(Player player){
getTeam().add(player);
}
public static final Creator<Team> CREATOR = new Creator<Team>() {
@Override
public Team createFromParcel(Parcel in) {
return new Team(in);
}
@Override
public Team[] newArray(int size) {
return new Team[size];
}
};
public String getTeamName() {
return team_name;
}
public String getSportName() { return sport_name; }
public String getTeamLeader() {
return team_leader;
}
public String getTrainingDay() {
return training_day;
}
public String getTrainingHour() {return training_hour; }
@Override
public String toString() {
return "Team{" +
"team_name='" + team_name + '\'' +
", sport_name='" + sport_name + '\'' +
", team_leader ='" + team_leader + '\'' +
", training_day=" + training_day +
", training_hour=" + training_hour +
", players=" + players +
'}';
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(team_name);
dest.writeString(sport_name);
dest.writeString(team_leader);
dest.writeString(training_day);
dest.writeString(training_hour);
System.out.println("in Team 3" +players);
dest.writeTypedList(players);
//System.out.println("in Team 3" +dest);
}
}
现在我给你上课类玩家
public class Player implements Parcelable {
private String playerName;
private String playerPosition;
private String playerMail;
private String playerPhone;
public Player(String _playerName){
this.playerName=_playerName;
this.playerPosition="";
this.playerMail = "";
this.playerPhone = "";
}
public Player(String _playerName, String _playerPosition, String _playerMail, String _playerPhone){
this.playerName = _playerName;
this.playerPosition = _playerPosition;
this.playerMail = _playerMail;
this.playerPhone = _playerPhone;
}
protected Player(Parcel in) {
playerName = in.readString();
playerPosition = in.readString();
playerMail = in.readString();
playerPhone = in.readString();
}
public static final Parcelable.Creator<Player> CREATOR = new Parcelable.Creator<Player>() {
@Override
public Player createFromParcel(Parcel in) {
return new Player(in);
}
@Override
public Player[] newArray(int size) {
return new Player[size];
}
};
@Override
public String toString() {
return "Player{" +
" PlayerName='" + playerName + '\'' +
", PlayerPosition='" + playerPosition + '\'' +
", PlayerMail=" + playerMail +
", PlayerPhone=" + playerPhone +
'}';
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(playerName);
dest.writeString(playerPosition);
dest.writeString(playerMail);
dest.writeString(playerPhone);
}
public String getPlayerName() {
if(playerName != null)
return playerName;
else
return "";
}
public String getPlayerPosition() {
if(playerPosition != null)
return playerPosition;
else
return "";
}
public String getPlayerMail() {
if(playerMail != null)
return playerMail;
else
return "";
}
public String getPlayerPhone() {
if(playerPhone != null)
return playerPhone;
else
return "";
}
}
如果你可以帮助我一点,或者将我重定向到有同样问题的人,那就很好了:)