Reputation: 433
I have a class that I am trying to parce using parcelable to use with an adaptor.
These are the items (in brief)
public class Stops implements Parcelable{
private String stop_id;
private String type;
private String location;
private String address;
private String city;
private String state;
private String zip;
private String from;
private String to;
private Commodities[] commodities;
.. etc etc etc
And the code to parce it:
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeArray(commodities);
}
private Stops(Parcel in){
commodities = in.readArray(); // the error is right here
}
public static final Creator<Stops> CREATOR = new Creator<Stops>(){
@Override
public Stops createFromParcel(Parcel source) {
return new Stops(source);
}
@Override
public Stops[] newArray(int size){
return new Stops[size];
}
};
I am getting an error on
private Stops(Parcel in){
commodities = in.readArray();
}
Some of the solutions involve changing dest.writeArray()
to dest.writeParcelable(etc etc
) although doing so
gives me a strange error saying that I am not passing a parcelable(commodities has its own class definition and it was made parcelable....which is why the error trips me out)
What would then be the best option to parce this?
The commodities class as requested:
public class Commodities implements Parcelable{
private String item;
private String description;
private String poNumber;
private String qty;
private String weight;
public Commodities() {}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPoNumber() {
return poNumber;
}
public void setPoNumber(String poNumber) {
this.poNumber = poNumber;
}
public String getQty() {
return qty;
}
public void setQty(String qty) {
this.qty = qty;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(item);
dest.writeString(description);
dest.writeString(poNumber);
dest.writeString(qty);
dest.writeString(weight);
}
private Commodities(Parcel in){
item = in.readString();
description =in.readString();
poNumber = in.readString();
qty = in.readString();
weight = in.readString();
}
public static final Creator<Commodities> CREATOR = new Creator<Commodities>() {
@Override
public Commodities createFromParcel(Parcel source) {
return new Commodities(source);
}
@Override
public Commodities[] newArray(int size) {
return new Commodities[size];}
};
}
And to give more context as to what I am trying to do. There are various stops(it is an array as well) which inside have an array of commodities. Both stops and commodities are populated accordingly. What I am trying to do is to send this information into a recyclerview adapter.
So each stop is an array with various data, part of that is another array of commodities.
Upvotes: 1
Views: 164
Reputation: 2320
You need call CREATER
for custom objects.
public class Stops implements Parcelable {
private String stop_id;
private String type;
private String location;
private String address;
private String city;
private String state;
private String zip;
private String from;
private String to;
private Commodities[] commodities;
protected Stops(Parcel in) {
stop_id = in.readString();
type = in.readString();
location = in.readString();
address = in.readString();
city = in.readString();
state = in.readString();
zip = in.readString();
from = in.readString();
to = in.readString();
commodities = in.createTypedArray(Commodities.CREATOR);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(stop_id);
dest.writeString(type);
dest.writeString(location);
dest.writeString(address);
dest.writeString(city);
dest.writeString(state);
dest.writeString(zip);
dest.writeString(from);
dest.writeString(to);
dest.writeTypedArray(commodities, flags);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<Stops> CREATOR = new Creator<Stops>() {
@Override
public Stops createFromParcel(Parcel in) {
return new Stops(in);
}
@Override
public Stops[] newArray(int size) {
return new Stops[size];
}
};
}
Upvotes: 1