Sai
Sai

Reputation: 2109

Parcelable not setting correct value for POJO field

I am using Retrofit to load data from backend. The POJO implements Parcelable. I am facing issues while reading nd writing to/from the POJO. I think it's because the field name is different from what I get from backend. Here's the 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);

    }

When I get POJO through intent.getParcelableExtra, the posterPath is null. What am I doing wrong.

Upvotes: 2

Views: 497

Answers (1)

Mauker
Mauker

Reputation: 11497

When working with Parcelable objects, you have to read the Parcel in the exact same order that you wrote it, otherwise it won't work.

So, if you wrote it like this:

dest.writeString("blah");
dest.writeInt(1);

you have to read it like this:

str = in.readString();
someInt = in.readInt();

More info on that on this article and on this tutorial.

This question, and this one here on SO also talk about Parcelable, with examples.

Upvotes: 2

Related Questions