我有一个类,其中有数组列表。如何通过意图发送它。以下我为我的班级做了
public class MakeOrder implements Parcelable {
@SerializedName("refno")
@Expose
private String refno;
@SerializedName("ddesc")
@Expose
private String ddesc;
@SerializedName("free")
@Expose
private String free;
@SerializedName("fgift")
@Expose
private String fgift;
@SerializedName("sgift")
@Expose
private String sgift;
@SerializedName("sandage")
@Expose
private SandageResponse sandage;
@SerializedName("inst")
@Expose
private String inst;
@SerializedName("items")
@Expose
private List<Item> items = new ArrayList<Item>();
@SerializedName("discount")
@Expose
private String discount;
@SerializedName("coupon")
@Expose
private Coupon coupon;
@SerializedName("delivery")
@Expose
private String delivery;
@SerializedName("user")
@Expose
private User user;
@SerializedName("otype")
@Expose
private String otype;
@SerializedName("ptype")
@Expose
private String ptype;
@SerializedName("app_id")
@Expose
private String appId;
@SerializedName("app_key")
@Expose
private String appKey;
@SerializedName("request")
@Expose
private String request;
@SerializedName("tablename")
@Expose
private String tablename;
@SerializedName("staff")
@Expose
private String staff;
public String getTablename() {
return tablename;
}
public void setTablename(String tablename) {
this.tablename = tablename;
}
public String getStaff() {
return staff;
}
public void setStaff(String staff) {
this.staff = staff;
}
public String getRefno() {
return refno;
}
public void setRefno(String refno) {
this.refno = refno;
}
public String getDdesc() {
return ddesc;
}
public void setDdesc(String ddesc) {
this.ddesc = ddesc;
}
public String getFree() {
return free;
}
public void setFree(String free) {
this.free = free;
}
public String getFgift() {
return fgift;
}
public void setFgift(String fgift) {
this.fgift = fgift;
}
public String getSgift() {
return sgift;
}
public void setSgift(String sgift) {
this.sgift = sgift;
}
public SandageResponse getSandage() {
return sandage;
}
public void setSandage(SandageResponse sandage) {
this.sandage = sandage;
}
public String getInst() {
return inst;
}
public void setInst(String inst) {
this.inst = inst;
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
public String getDiscount() {
return discount;
}
public void setDiscount(String discount) {
this.discount = discount;
}
public Coupon getCoupon() {
return coupon;
}
public void setCoupon(Coupon coupon) {
this.coupon = coupon;
}
public String getDelivery() {
return delivery;
}
public void setDelivery(String delivery) {
this.delivery = delivery;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getOtype() {
return otype;
}
public void setOtype(String otype) {
this.otype = otype;
}
public String getPtype() {
return ptype;
}
public void setPtype(String ptype) {
this.ptype = ptype;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getAppKey() {
return appKey;
}
public void setAppKey(String appKey) {
this.appKey = appKey;
}
public String getRequest() {
return request;
}
public void setRequest(String request) {
this.request = request;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.refno);
dest.writeString(this.ddesc);
dest.writeString(this.free);
dest.writeString(this.fgift);
dest.writeString(this.sgift);
dest.writeParcelable(this.sandage, flags);
dest.writeString(this.inst);
dest.writeTypedList(this.items);
dest.writeString(this.discount);
dest.writeParcelable(this.coupon, flags);
dest.writeString(this.delivery);
dest.writeParcelable(this.user, flags);
dest.writeString(this.otype);
dest.writeString(this.ptype);
dest.writeString(this.appId);
dest.writeString(this.appKey);
dest.writeString(this.request);
dest.writeString(this.tablename);
dest.writeString(this.staff);
}
public MakeOrder() {
}
protected MakeOrder(Parcel in) {
this.refno = in.readString();
this.ddesc = in.readString();
this.free = in.readString();
this.fgift = in.readString();
this.sgift = in.readString();
this.sandage = in.readParcelable(SandageResponse.class.getClassLoader());
this.inst = in.readString();
this.items = in.createTypedArrayList(Item.CREATOR);
this.discount = in.readString();
this.coupon = in.readParcelable(Coupon.class.getClassLoader());
this.delivery = in.readString();
this.user = in.readParcelable(User.class.getClassLoader());
this.otype = in.readString();
this.ptype = in.readString();
this.appId = in.readString();
this.appKey = in.readString();
this.request = in.readString();
this.tablename = in.readString();
this.staff = in.readString();
}
public static final Parcelable.Creator<MakeOrder> CREATOR = new Parcelable.Creator<MakeOrder>() {
@Override
public MakeOrder createFromParcel(Parcel source) {
return new MakeOrder(source);
}
@Override
public MakeOrder[] newArray(int size) {
return new MakeOrder[size];
}
};
}
请查看更新的 我收到错误消息,例如无法编组值 我做了所有类Parcelable。如何通过意图发送其对象 提前致谢
Logcat就像
java.lang.RuntimeException: Parcel: unable to marshal value Models.PlaceOrder.Sub@41e10f80
at android.os.Parcel.writeValue(Parcel.java:1235)
at android.os.Parcel.writeList(Parcel.java:622)
at Models.PlaceOrder.Item.writeToParcel(Item.java:90)
at android.os.Parcel.writeTypedList(Parcel.java:1017)
at Models.PlaceOrder.MakeOrder.writeToParcel(MakeOrder.java:246)
at android.os.Parcel.writeParcelable(Parcel.java:1254)
at android.os.Parcel.writeValue(Parcel.java:1173)
at android.os.Parcel.writeMapInternal(Parcel.java:591)
at android.os.Bundle.writeToParcel(Bundle.java:1627)
at android.os.Parcel.writeBundle(Parcel.java:605)
at android.content.Intent.writeToParcel(Intent.java:6866)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1897)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1418)
at android.app.Activity.internalStartActivityForResult(Activity.java:3427)
at android.app.Activity.access$200(Activity.java:660)
at android.app.Activity$2.onStartActivity(Activity.java:3417)
at android.app.Activity.startActivityForBusiness(Activity.java:5441)
at android.app.Activity.startActivityForResult(Activity.java:3413)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
at android.app.Activity.startActivityForResult(Activity.java:3367)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:871)
at android.app.Activity.startActivity(Activity.java:3644)
at android.app.Activity.startActivity(Activity.java:3612)
Items.java 获取异常
public class Item implements Parcelable {
@SerializedName("itemid")
@Expose
private String itemid;
@SerializedName("qty")
@Expose
private String qty;
@SerializedName("sub")
@Expose
private List<Sub> sub = new ArrayList<Sub>();
/**
*
* @return
* The itemid
*/
public String getItemid() {
return itemid;
}
/**
*
* @param itemid
* The itemid
*/
public void setItemid(String itemid) {
this.itemid = itemid;
}
/**
*
* @return
* The qty
*/
public String getQty() {
return qty;
}
/**
*
* @param qty
* The qty
*/
public void setQty(String qty) {
this.qty = qty;
}
/**
*
* @return
* The sub
*/
public List<Sub> getSub() {
return sub;
}
/**
*
* @param sub
* The sub
*/
public void setSub(List<Sub> sub) {
this.sub = sub;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.itemid);
dest.writeString(this.qty);
dest.writeList(this.sub);
}
public Item() {
}
protected Item(Parcel in) {
this.itemid = in.readString();
this.qty = in.readString();
this.sub = new ArrayList<Sub>();
in.readList(this.sub, Sub.class.getClassLoader());
}
public static final Parcelable.Creator<Item> CREATOR = new Parcelable.Creator<Item>() {
@Override
public Item createFromParcel(Parcel source) {
return new Item(source);
}
@Override
public Item[] newArray(int size) {
return new Item[size];
}
};
}
答案 0 :(得分:2)
您还需要在项目和优惠券对象中实施Parcelable 。
然后当您在MakeOrder类中添加并阅读列表时:
@Override
public void writeToParcel(Parcel dest, int flags) {
...
// to write the Item list
dest.writeList(items);
// to write Coupon object
dest.writeParcelable(coupon, falgs);
}
BTW,你的构造函数是空的
private MakeOrder(Parcel in) {
...
// to read the Item list
items = new ArrayList<Item>();
in.readList(items, Item.CREATOR);
// to read Coupon object
in.readParcelable(Coupon.CREATOR);
}
此链接可以帮助您:
这是Parcelable文档:
https://developer.android.com/reference/android/os/Parcel.html
修改强>
问题中添加了更多Percel问题。
聊天中的最终解决方案:http://chat.stackoverflow.com/rooms/123710/discussion-between-aman-singh-and-adalpari
答案 1 :(得分:1)
你需要加入这样的额外内容:
Intent intent-new Intent(this,YouActvity.class);
intent.putExtra("key",yourModel);
startActivity(intent);
在要回溯的活动中:
YouModel yourModel = (YourModel) getIntent().getSerializableExtra("key");
答案 2 :(得分:0)
尝试使用此
intent.putParcelableArrayListExtra("value", orderList);
在其他活动中将其视为:
List<String> orderList=this.getIntent().
getParcelableArrayListExtra("value");
希望这有帮助
答案 3 :(得分:0)
首先,您的Parcelable
构造函数为空。如果你把它留空,那它就不会起作用。您需要以与writeToParcel()
方法中编写的顺序相同的方式读取parcel中的字段。
然后将parcelable数据写入意图的putExtra()
方法。要从意图中提取数据,请使用意图
getParcelableExtra()
方法