Android:如何实现Parcelable到我的对象?

我有这个持有我的JSON索引的ConstantData类,我需要在具有额外的活动之间传递它们。 但在此之前,我必须首先实现Parcelable到这个类的对象。

我的问题是,我应该如何在这里声明我的类中的对象,并把每个对象内的variables?

我是一个新手,我现在完全无能为力。 谢谢。 随意修改下面的代码。

ConstantData.java

public class ConstantData{ public static String project_title = "project title"; public static String organization_title = "organization title"; public static String keyword = "keyword"; public static String short_code = "short code"; public static String project_description = "description"; public static String smallImageUrl = "smallImageUrl"; public static String bigImageUrl = "bigImageUrl"; public static String price= "price"; public static String country= "country"; public static ArrayList<Project> projectsList = new ArrayList<Project>(); public int describeContents() { return 0; } public void writeToParcel(Parcel out, int flags) { out.writeString(project_title); out.writeString(organization_title); out.writeString(keyword); out.writeString(short_code); out.writeString(project_description); out.writeString(smallImageUrl); out.writeString(bigImageUrl); out.writeString(price); out.writeString(country); } public static final Parcelable.Creator<ConstantData> CREATOR = new Parcelable.Creator<ConstantData>() { public ConstantData createFromParcel(Parcel in) { return new ConstantData(in); } public ConstantData[] newArray(int size) { return new ConstantData[size]; } }; private ConstantData(Parcel in) { project_title = in.readString(); organization_title = in.readString(); keyword = in.readString(); short_code = in.readString(); project_description = in.readString(); smallImageUrl = in.readString(); bigImageUrl = in.readString(); price = in.readString(); country = in.readString(); } } 

如果我的问题不够清楚,你可以看看这个问题: 如何使用Intents从一个Android活动发送对象到另一个?

在那里,他写了myParcelableObject ,我只是不知道如何使可parcelable对象。

编辑 Project.java

 public class Project { public String project_title; public String organization_title; public String keyword; public String short_code; public String project_description; public String smallImageUrl; public String bigImageUrl; public String price; public String country; } 

Solutions Collecting From Web of "Android:如何实现Parcelable到我的对象?"

您需要实现Parcelable接口

 public class ConstantData implements Parcelable { public void writeToParcel(Parcel out, int flags) { out.writeString(project_title); .... out.writeString(country); out.writeList(projectsList); // <<-- } private ConstantData(Parcel in) { project_title = in.readString(); .... country = in.readString(); projectsList = in.readList(); 

我认为对于要编写的projectsListProject类也需要实现Parcelable

看例如这个类的例子。