我想将模型对象发送到另一个活动,其中包含列表引用另一个模型
public class row_my_bookings implements Parcelable {
String BookingID;
ArrayList<MainService> list_of_mainservice;
public row_my_bookings(Parcel in) {
String[] data = new String[3];
in.readStringArray(data);
this.list_of_mainservice = in.readArrayList(null);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(this.list_of_mainservice); // Error shows in this line
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public row_my_bookings createFromParcel(Parcel in) {
return new row_my_bookings(in);
}
public row_my_bookings[] newArray(int size) {
return new row_my_bookings[size];
}
};
}
row_main_services模型类
public class row_main_services implements Parcelable{
String ServiceId,
public MainService(Parcel in) {
String[] data = new String[3];
in.readStringArray(data);
this.id = in.readString();
this.name=in.readString();
if (in.readByte() == 0x01) {
list_child_services = new ArrayList<row_child_services>();
in.readList(list_child_services, row_child_services.class.getClassLoader());
} else {
list_child_services = null;
}
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.id);
dest.writeString(this.name);
if (list_child_services == null) {
dest.writeByte((byte) (0x00));
} else {
dest.writeByte((byte) (0x01));
dest.writeList(list_child_services);
}
}
public static final Parcelable.Creator<MainService> CREATOR = new Parcelable.Creator<MainService>() {
public MainService createFromParcel(Parcel in) {
return new MainService(in);
}
public MainService[] newArray(int size) {
return new MainService[size];
}
};
第三个模块
public class row_child_services implements Parcelable{
String subId,subName;
public row_child_services(Parcel in) {
String[] data = new String[3];
in.readStringArray(data);
this.subId = in.readString();
this.subName = in.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.subId);
dest.writeString(this.subName);
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public row_child_services createFromParcel(Parcel in) {
return new row_child_services(in);
}
public row_child_services[] newArray(int size) {
return new row_child_services[size];
}
};
我按照以下方式发送对象
list_Bookings=new ArrayList<row_my_bookings>(rowItems);
intentBookingWindow.putParcelableArrayListExtra("BookingObject", list_Bookings);
答案 0 :(得分:0)
row_my_bookings
类实现Parcelable
,但我猜MainService
类没有实现Parcelable
,这就是你的问题。你也必须上课Parcelable
。
我重新命令你改变你的模型: 您已导入“MainService”类,该类不是您稍后发送的类。我想你必须把这个类改成row_main_services,然后重新考虑你的Parcelable实现;)
public class row_my_bookings implements Parcelable {
String BookingID;
ArrayList<row_main_services> list_of_mainservice; // i guess you must change this line
protected row_my_bookings(Parcel in) {
BookingID = in.readString();
if (in.readByte() == 0x01) {
list_of_mainservice = new ArrayList<MainService>();
in.readList(list_of_mainservice, MainService.class.getClassLoader());
} else {
list_of_mainservice = null;
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(BookingID);
if (list_of_mainservice == null) {
dest.writeByte((byte) (0x00));
} else {
dest.writeByte((byte) (0x01));
dest.writeList(list_of_mainservice);
}
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<row_my_bookings> CREATOR = new Parcelable.Creator<row_my_bookings>() {
@Override
public row_my_bookings createFromParcel(Parcel in) {
return new row_my_bookings(in);
}
@Override
public row_my_bookings[] newArray(int size) {
return new row_my_bookings[size];
}
};
}
clss row_main_services:
public class row_main_services implements Parcelable {
String ServiceId,
ArrayList<row_child_services> list_row_child_services;
protected row_main_services(Parcel in) {
if (in.readByte() == 0x01) {
list_row_child_services = new ArrayList<row_child_services>();
in.readList(list_row_child_services, row_child_services.class.getClassLoader());
} else {
list_row_child_services = null;
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
if (list_row_child_services == null) {
dest.writeByte((byte) (0x00));
} else {
dest.writeByte((byte) (0x01));
dest.writeList(list_row_child_services);
}
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<row_main_services> CREATOR = new Parcelable.Creator<row_main_services>() {
@Override
public row_main_services createFromParcel(Parcel in) {
return new row_main_services(in);
}
@Override
public row_main_services[] newArray(int size) {
return new row_main_services[size];
}
};
}
这是你的第三堂课:
public class row_child_services implements Parcelable {
String subId;
String subName;
protected row_child_services(Parcel in) {
subId = in.readString();
subName = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(subId);
dest.writeString(subName);
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<row_child_services> CREATOR = new Parcelable.Creator<row_child_services>() {
@Override
public row_child_services createFromParcel(Parcel in) {
return new row_child_services(in);
}
@Override
public row_child_services[] newArray(int size) {
return new row_child_services[size];
}
};
}
关于你上次犯的错误,我建议你看看this回答。