你如何实际创建一个实现Parcelable的类的对象?网上有很多例子可以准确显示这个类应该是什么样子,但我似乎无法找到关于如何创建对象的东西。
如果我有一个实现Parcelable的类,并且构造函数看起来像这样:
public class ResultForTeam implements Parcelable
{
String playerA, playerB, teamA, teamB, scores;
int playerA_games, playerB_games;
public ResultForTeam(Parcel in)
{
this.playerA = in.readString();
this.playerB = in.readString();
this.teamA = in.readString();
this.teamB = in.readString();
this.scores = in.readString();
this.playerA_games = in.readInt();
this.playerB_games = in.readInt();
}
如何在我的代码中的其他地方创建此类的对象? 在我实现Parcelable之前,它曾经是这样的。
ResultForTeam newResult = new ResultForTeam(playerA, playerB, teamA, teamB, scores, playerA_games, playerB_games);
答案 0 :(得分:1)
您可以保留旧的构造函数,并使其重载。您还需要实施describeContents()
和writeToParcel()
方法。别忘了写静态CREATOR
字段。然后,您将能够像往常一样使用该类,并将其与Parcel
一起使用。