【问题标题】:How to implement the write & read in Parcelable ClassParcelable Class中如何实现读写
【发布时间】: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


【解决方案1】:

写卡片

out.writeParcelable(mCard, flags);

读卡

mCard = (Card) in.readParcelable(Card.class.getClassLoader());

写布尔值

out.writeInt(mLifeStatus ? 1 : 0);
out.writeInt(mProtected ? 1 : 0);

读取布尔值

mLifeStatus = in.readInt() == 1;
mProtected = in.readInt() == 1;

(这就是 writeValuereadValue 在内部为 Boolean 类型工作的方式)

【讨论】:

    【解决方案2】:

    Parcel 可以存储原始类型和 Parcelable 对象。这意味着存储在 Parcel 中的任何内容都必须是原始类型或 Parceelable 对象。

    查看 Player 的成员数据,我看到一堆原始类型和一个更复杂的类型:Card。

    要将 Card 存储在 Parcel 中,您必须将 Card 类设为 Parcelable。

    或者,如果 Player 类可以访问 Card 的内部细节,您可以编写代码从 Card 中提取原始类型并存储它们,然后在读取端,从 Parcel 中提取原始类型并使用它们构造一张卡片。此技术仅适用于 Card 足够简单以至于您不必担心违反封装的情况。

    【讨论】:

      【解决方案3】:

      writeToParcel:

      dest.writeByte((byte) (myBoolean ? 1 : 0));  
      

      从包裹中读取:

      myBoolean = in.readByte() != 0; 
      

      参考:https://stackoverflow.com/a/7089687/4577267

      【讨论】:

      • 请不要公然复制粘贴答案。另外,您没有完全回答问题。
      猜你喜欢
      • 2011-09-06
      • 2020-12-31
      • 1970-01-01
      • 2011-10-25
      • 2020-03-27
      • 2015-07-07
      • 1970-01-01
      • 2018-01-04
      • 1970-01-01
      相关资源
      最近更新 更多