我有一个类,我试图使用parcelable与适配器一起使用。
这些是项目(简要说明)
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
以及代替它的代码:
@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];
}
};
我在
上收到错误private Stops(Parcel in){
commodities = in.readArray();
}
有些解决方案涉及将dest.writeArray()
更改为dest.writeParcelable(etc etc
),尽管这样做
给了我一个奇怪的错误,说我没有通过一个可分解的商品(商品有自己的类别定义,而且它是可以分配的......这就是为什么错误会把我绊倒)
什么是最好的选择?
所要求的商品类别:
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];}
};
}
并提供更多关于我想要做的事情。有各种各样的停靠点(它也是一个数组)里面有一系列的商品。两个站点和商品都相应地填充。我想要做的是将此信息发送到recyclerview适配器。
所以每个站点都是一个包含各种数据的数组,其中一部分是另一组商品。
答案 0 :(得分:1)
您需要为自定义对象调用CREATER
。
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];
}
};
}