2

我想将对象从一个类传递到另一个类,我的代码是

    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");

甚至我保留了我的班级实现可序列化

我在声明 arraylist 时击中 ed 的班级

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);
}

} }

4

3 回答 3

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());
于 2013-07-09T08:41:43.460 回答
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);
于 2013-07-09T06:57:29.773 回答
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();
    }
}

请注意,如果您要从给定 Parcel 中检索多个字段,则必须按照放入它们的相同顺序执行此操作(即,采用 FIFO 方法)。

实现对象后,只需使用putExtra()Parcelable将它们放入Intent即可:

Intent i = new Intent();
i.putExtra("name_of_extra", myParcelableObject);

然后你可以用getParcelableExtra()把它们拉出来:

Intent i = getIntent();
MyParcelable myParcelableObject = (MyParcelable) i.getParcelableExtra("name_of_extra");

希望这将指导和帮助你。:)

于 2013-07-09T07:03:30.410 回答