小编典典

Android:如何对我的对象实施Parcelable?

java

我有一个ConstantData类,该类保存我的JSON索引,我需要在活动之间附加传递它们。但在此之前,我必须首先对此类的对象实现Parcelable。

我的问题是,如何在这里的类中声明对象并将每个对象放入变量中?

我是新手,现在我一无所有。谢谢。请随意在下面修改我的代码。

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

如果我的问题不够清楚,您可以查找以下问题:如何使用Intent将对象从一个Android Activity发送到另一个Android
Activity?

myParcelableObject在那儿写道,我只是不知道如何制作可包裹的物体。

编辑 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;
 }

阅读 222

收藏
2020-09-23

共1个答案

小编典典

您需要实现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();

我认为为了写作的projectsList工作,Project班级也需要实施Parcelable

例如,请参见此类

2020-09-23