我想将对象从一个类传递给另一个类,我的代码是
private MemberLoginResponse loginResponse;
.
.
Intent intent = new Intent();
intent = new Intent(context, BillingDeskActivity.class);
intent.putExtra("privilages", loginResponse);
startActivity(intent);
在接收数据的其他活动中,我使用此代码
Intent i_privilages = getIntent();
privilages = (MemberLoginResponse) getIntent().getSerializableExtra("privilages");
甚至我让我的班级实现了Serializable
我的课程,我在宣布arraylist
时受到了抨击public class MemberLoginResponse implements Parcelable {
public Integer merchantMemberId = 0;
public Integer merchantId = 0;
public String merchantName = "";
public String merchantLogo = "";
public String merchantType = "";
public String merchantHQAddress = "";
public List<CustomSystemPrivilege> merchantPrivileges;
public List<CustomSystemPrivilege> privileges;
public StatusCode statusCode;
public Integer branchId = 0;
public MemberLoginResponse(Parcel in) {
merchantMemberId = in.readInt();
merchantId = in.readInt();
merchantName = in.readString();
merchantLogo = in.readString();
merchantType = in.readString();
merchantHQAddress = in.readString();
in.readList(merchantPrivileges,
CustomSystemPrivilege.class.getClassLoader());
in.readList(privileges, CustomSystemPrivilege.class.getClassLoader());
branchId = in.readInt();
}
public MemberLoginResponse() {
// TODO Auto-generated constructor stub
}
public static final Parcelable.Creator<MemberLoginResponse> CREATOR = new Parcelable.Creator<MemberLoginResponse>() {
@Override
public MemberLoginResponse createFromParcel(Parcel in) {
return new MemberLoginResponse(in);
}
@Override
public MemberLoginResponse[] newArray(int size) {
return new MemberLoginResponse[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(merchantMemberId);
dest.writeInt(merchantId);
dest.writeString(merchantName);
dest.writeString(merchantLogo);
dest.writeString(merchantType);
dest.writeString(merchantHQAddress);
dest.writeList(merchantPrivileges);
dest.writeList(privileges);
dest.writeInt(branchId);
}
} }
答案 0 :(得分:2)
以下是将另一个parcelable对象读/写为parcelable的代码。
class MemberLoginResponse implements Parcelable {
//
private StatusCode statusCode;
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(statusCode , flags);
}
}
如果你想阅读它,请使用此
statusCode = (StatusCode)in.readParcelable(A.class.getClassLoader());
答案 1 :(得分:0)
以下列方式尝试
public class clsChart implements Parcelable{
private int chartID;
private String chartName;
private int chartType;
public clsChart(){
}
@SuppressWarnings("rawtypes")
public static final Parcelable.Creator CREATOR =
new Parcelable.Creator() {
@Override
public clsChart createFromParcel(Parcel in) {
return new clsChart(in);
}
@Override
public clsChart[] newArray(int size) {
return new clsChart[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(chartID);
dest.writeString(chartName);
dest.writeInt(chartType);
}
private void readFromParcel(Parcel in) {
chartID = in.readInt();
chartName = in.readString();
chartType = in.readInt();
}
public int getChartID() {
return chartID;
}
public void setChartID(int chartID) {
this.chartID = chartID;
}
public String getChartName() {
return chartName;
}
public void setChartName(String chartName) {
this.chartName = chartName;
}
public int getChartType() {
return chartType;
}
public void setChartType(int chartType) {
this.chartType = chartType;
}
public clsChart(Parcel in) {
readFromParcel(in);
}
public clsChart( int chartID,String chartName,int chartType)
{
this.chartID = chartID;
this.chartName = chartName;
this.chartType = chartType;
}
}
now Bundle b=new Bundle();
b.putParcelable("your_key",your_object_which_implements_parcelable(here chart object));
intent.putExtras(b);
答案 2 :(得分:0)
如果您只是传递物体,那么Parcelable就是为此设计的。与使用Java的本机序列化相比,它需要更多的努力才能使用,但速度更快。
从文档中,一个关于如何实现的简单示例是:
// simple class that just has one member property as an example
public class MyParcelable implements Parcelable {
private int mData;
/* everything below here is for implementing Parcelable */
// 99.9% of the time you can just ignore this
public int describeContents() {
return 0;
}
// write your object's data to the passed-in Parcel
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
// this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
// example constructor that takes a Parcel and gives you an object populated with it's values
private MyParcelable(Parcel in) {
mData = in.readInt();
}
}
注意,如果您要从给定的包中检索多个字段,则必须按照放入它们的顺序(即采用FIFO方法)执行此操作。
一旦您的对象实施Parcelable
,只需将Intents putExtra()放入getParcelableExtra():
Intent i = new Intent();
i.putExtra("name_of_extra", myParcelableObject);
然后您可以使用{{3}}
将其撤回Intent i = getIntent();
MyParcelable myParcelableObject = (MyParcelable) i.getParcelableExtra("name_of_extra");
希望这会引导&amp;帮你。 :)