将parcelable数据从一个活动传递到另一个活动,而在Activity2中我需要获取传递的数据。部分修改(arraylist部分)
我设置了这些值 年份19.99 虽然收到这样的结果 null null 1.3796130171011302E-306
Intent intent = new Intent(getActivity(), Activity2.class);
intent.putExtra("SELECTED_ITEM", item);
public class Activity2 extends FragmentActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent data = getIntent();
selectedItem = data.getExtras().getParcelable("SELECTED_ITEM");
}
}
@Element(name = "data")
public class Data implements Parcelable {
@Element(name = "ID")
private String id;
@ElementList(name = "iap_subscription_packages", required = false)
private ArrayList<Subscription> iapSubscription;
Data() {
}
Data(Parcel in) {
readFromParcel(in);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public ArrayList<Subscription> getSubscription() {
return subscription;
}
public void setSubscription(ArrayList<Subscription> subscription) {
this.iapSubscription = iapSubscription;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeList(iapSubscription);
}
private void readFromParcel(Parcel in) {
id = in.readString();
iapSubscription = new ArrayList<Subscription>();
if (iapSubscription != null) {
in.readTypedList(iapSubscription, Subscription.CREATOR);
}
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Data createFromParcel(Parcel in) {
return new Data(in);
}
public Data[] newArray(int size) {
return new Data[size];
}
};
}
@Element(name = "package")
public class Subscription implements Parcelable {
Subscription() {
}
Subscription(Parcel in) {
readFromParcel(in);
}
@Element(name = "cycle", required = false)
private String cycle;
@Element(name = "name", required = false)
private String name;
@Element(name = "price", required = false)
private double subscriptionPrice;
public String getCycle() {
return cycle;
}
public void setCycle(String cycle) {
this.cycle = cycle;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSubscriptionPrice() {
return subscriptionPrice;
}
public void setSubscriptionPrice(double subscriptionPrice) {
this.subscriptionPrice = subscriptionPrice;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(cycle);
dest.writeString(name);
dest.writeDouble(subscriptionPrice);
Log.i("subscription", "writeToParcel " + cycle + " " + name + " " + subscriptionPrice);
}
private void readFromParcel(Parcel in) {
cycle = in.readString();
name = in.readString();
subscriptionPrice = in.readDouble();
Log.i("subscription", "readFromParcel " + cycle + " " + name + " " + subscriptionPrice);
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Subscription createFromParcel(Parcel in) {
return new Subscription(in);
}
public Subscription[] newArray(int size) {
return new Subscription[size];
}
};
}
答案 0 :(得分:1)
您发布的代码是您实际测试的内容吗?
此方法有错误:
public void setSubscription(ArrayList<Subscription> subscription) {
this.iapSubscription = iapSubscription;
}
你可能想要这个:
public void setSubscription(ArrayList<Subscription> subscription) {
this.iapSubscription = subscription;
}
我复制了你的代码并测试了与Parceleable相关的部分。这似乎是正确的。我能够打包和取消包含订阅的ArrayList。
如果您仍然遇到问题,请发布您为意图构建捆绑包的代码。