Parcelable 没有为 POJO 字段设置正确的值
Parcelable not setting correct value for POJO field
我正在使用 Retrofit 从后端加载数据。 POJO 实现了 Parcelable。我在阅读和编写 to/from POJO 时遇到问题。我认为这是因为字段名称与我从后端获得的字段名称不同。这是 POJO:
@SerializedName("poster_path")
public String posterPath;
....
private Movie(Parcel in) {
...
posterPath= in.readString();
...
}
...//more code
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(posterPath);
}
当我通过intent.getParcelableExtra获取POJO时,posterPath为空。我做错了什么。
使用 Parcelable
对象时,您必须按照编写时完全相同的顺序阅读 Parcel
,否则将无法工作。
所以,如果你这样写:
dest.writeString("blah");
dest.writeInt(1);
你必须这样读:
str = in.readString();
someInt = in.readInt();
有关 this article and on this tutorial 的更多信息。
This question, and this one这里就SO也讲一下Parcelable
,有例子。
我正在使用 Retrofit 从后端加载数据。 POJO 实现了 Parcelable。我在阅读和编写 to/from POJO 时遇到问题。我认为这是因为字段名称与我从后端获得的字段名称不同。这是 POJO:
@SerializedName("poster_path")
public String posterPath;
....
private Movie(Parcel in) {
...
posterPath= in.readString();
...
}
...//more code
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(posterPath);
}
当我通过intent.getParcelableExtra获取POJO时,posterPath为空。我做错了什么。
使用 Parcelable
对象时,您必须按照编写时完全相同的顺序阅读 Parcel
,否则将无法工作。
所以,如果你这样写:
dest.writeString("blah");
dest.writeInt(1);
你必须这样读:
str = in.readString();
someInt = in.readInt();
有关 this article and on this tutorial 的更多信息。
This question, and this one这里就SO也讲一下Parcelable
,有例子。