【发布时间】:2016-10-22 05:42:31
【问题描述】:
可打包
我有这个Player 班级:
public class Player implements Parcelable {
private String mName; // Player's name
private Card mCard; // Player's current card
private boolean mLifeStatus = true; // Player's life status
private boolean mProtected = false; // If the Player's been protected by the guard or not
private int mId; // ID of the Player
private int mCount;
/* everything below here is for implementing Parcelable */
// 99.9% of the time you can just ignore this
@Override
public int describeContents() {
return 0;
}
// write your object's data to the passed-in Parcel
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(mName);
out.writeValue(mCard);
out.writeValue(mLifeStatus);
out.writeValue(mProtected);
out.writeInt(mId);
out.writeInt(mCount);
}
// this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
public static final Parcelable.Creator<Player> CREATOR = new Parcelable.Creator<Player>() {
public Player createFromParcel(Parcel in) {
return new Player(in);
}
public Player[] newArray(int size) {
return new Player[size];
}
};
// example constructor that takes a Parcel and gives you an object populated with it's values
private Player(Parcel in) {
mName = in.readString();
mCard = in.readValue();
mLifeStatus = in.readValue(mLifeStatus);
mProtected = in.readValue(mProtected);
mId = in.readInt();
mCount = in.readInt();
}
}
我尝试自己填充最后一个构造函数,但我不知道如何读取布尔值和自定义类的值,就像我的 Card 类一样,它是 mValue @987654324 的类@。
我试过用这个但还是不行:mCard = in.readValue(Card.class.getClassLoader);
我应该如何编写这两个方法以使 Class 实现 Parcelable 它应该是什么?
【问题讨论】:
-
Card是否实现了Parcelable?如果没有,则需要
标签: java android parcelable