0

I have created the following parcelable object:

public class ViewModel implements Parcelable {

private String image, price, credit, title, description, id;

public ViewModel(String image, String price, String credit, String title, String description, String id) {
    this.image = image;
    this.price = price;
    this.credit = credit;
    this.title = title;
    this.description = description;
    this.id = id;
}

public String getPrice() {
    return price;
}

public String getCredit() {
    return credit;
}

public String getDescription() {
    return description;
}

public String getId() {
    return id;
}

public String getTitle() {
    return title;
}

public String getImage() {
    return image;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeStringArray(new String[] {
            this.image,
            this.price,
            this.credit,
            this.title,
            this.description,
            this.id
    });
}

/** Static field used to regenerate object, individually or as arrays */
public static final Parcelable.Creator<ViewModel> CREATOR = new Parcelable.Creator<ViewModel>() {
    public ViewModel createFromParcel(Parcel pc) {
        return new ViewModel(pc);
    }
    public ViewModel[] newArray(int size) {
        return new ViewModel[size];
    }
};

/**Creator from Parcel, reads back fields IN THE ORDER they were written */
public ViewModel(Parcel pc){
    image = pc.readString();
    price = pc.readString();
    credit = pc.readString();
    title = pc.readString();
    description = pc.readString();
    id = pc.readString();
}

}

Now I am sending an ArrayList of ViewModel through bundle:

 bundle.putParcelableArrayList("products", viewModels);

Is there anything wrong I am doing? Because I get null Bundle Arguments, but if I send a simple string, everything works.

Filip Luchianenco
  • 6,912
  • 9
  • 41
  • 63

2 Answers2

0

make this change:

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.image);
    dest.writeString(this.price);
    dest.writeString(this.credit);
    dest.writeString(this.title);
    dest.writeString(this.description);
    dest.writeString(this.id);
}


getIntent().getParcelableArrayListExtra("product"); // get the list
rainash
  • 864
  • 6
  • 15
0

You are trying to retrieve a parcelable ArrayList. You need to retrive a parcelable ViewModel object that you created. Change:

bundle.putParcelableArrayList("product", viewModels);

and

getIntent().getParcelableArrayList("product");

to:

bundle.putParcelable("product", viewModels);

and

getIntent().getParcelable("product"); 
Chisko
  • 3,092
  • 6
  • 27
  • 45