我正在使用 Retrofit 从后端加载数据。 POJO 实现了 Parcelable。我在 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为null。我做错了什么。
最佳答案
使用 Parcelable
时对象,您必须按照与写入的顺序完全相同的顺序读取 Parcel
,否则它将无法工作。
所以,如果你这样写:
dest.writeString("blah");
dest.writeInt(1);
你必须这样读:
str = in.readString();
someInt = in.readInt();
更多信息请访问 this article以及 this tutorial .
This question ,和this one这里还讨论了 Parcelable
,并附有示例。
关于android - Parcelable 未为 POJO 字段设置正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35564260/