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

2023-09-12 08:09:41 作者:给力小青年、

我有抓住我的JSON指标这个ConstantData类,我需要用额外活动之间传递它们。但在此之前,我要实现Parcelable这一类的对象第一。

我的问题是,我应该怎么我的类中声明的对象,在这里,并把每一个对象在变量中?

我是新手,我完全一无所知现在。谢谢。随意修改我的code以下。

ConstantData.java

 公共类ConstantData {

       公共静态字符串PROJECT_TITLE =项目名称;
       公共静态字符串organization_title =组织称号;
       公共静态字符串关键字=关键词;
       公共静态字符串short_ code =短code;
       公共静态字符串project_description =说明;
       公共静态字符串smallImageUrl =smallImageUrl;
       公共静态字符串bigImageUrl =bigImageUrl;
       公共静态字符串价=价格;
       公共静态字符串全国=国家;



        公共静态的ArrayList<项目> projectsList =新的ArrayList<项目>();


        公众诠释describeContents(){
            返回0;
        }

        公共无效writeToParcel(包裹出来,诠释标志){
            out.writeString(PROJECT_TITLE);
            out.writeString(organization_title);
            out.writeString(关键字);
            out.writeString(short_ code);
            out.writeString(project_description);
            out.writeString(smallImageUrl);
            out.writeString(bigImageUrl);
            out.writeString(价格);
            out.writeString(国家);
        }

        公共静态最终Parcelable.Creator< ConstantData> CREATOR
                =新Parcelable.Creator< ConstantData>(){
            公共ConstantData createFromParcel(包裹中){
                返回新ConstantData(中);
            }

            公共ConstantData [] newArray(INT尺寸){
                返回新ConstantData【尺寸】;
            }
        };

        私人ConstantData(包裹中){
            PROJECT_TITLE = in.readString();
            organization_title = in.readString();
            关键字= in.readString();
            short_ code = in.readString();
            project_description = in.readString();
            smallImageUrl = in.readString();
            bigImageUrl = in.readString();
            价格= in.readString();
            国家= in.readString();
        }
    }
 

在的情况下,我的问题是不够清楚,你可以看一下这个问题:How从一个Android的活动将对象到另一个Intent.putExtra?

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

修改 * Project.java 的*

 公共类项目{

    公共字符串PROJECT_TITLE;
    公共字符串organization_title;
    公共字符串关键字;
    公共字符串short_ code;
    公共字符串project_description;
    公共字符串smallImageUrl;
    公共字符串bigImageUrl;
    公共字符串的价格;
    公共字符串的国家;
 }
 
Android parcelable 解析

解决方案

您需要执行 Parcelable 接口

 公共类ConstantData实现Parcelable {

    公共无效writeToParcel(包裹出来,诠释标志){
        out.writeString(PROJECT_TITLE);
    ....
        out.writeString(国家);
        out.writeList(projectsList); //<<  - 
    }

    私人ConstantData(包裹中){
        PROJECT_TITLE = in.readString();
        ....
        国家= in.readString();
        projectsList = in.readList();
 

我觉得对的 projectsList 工作,项目类还需要实现 Parcelable 。

请参阅例如this类一个例子。

I have this ConstantData class which holds my JSON indexes, I need to pass them between activities with extras. But before that, I have to implement Parcelable to the objects of this class first.

My question is, how should I declare the object within my class here and put every object inside a variable?

I'm a newbie and I'm totally clueless right now. Thank you. Feel free to modify my code below.

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

In case my question is not clear enough, you can look up this question: How to send an object from one Android Activity to another with Intent.putExtra?

There he wrote myParcelableObject, I just don't know how to make the parcelable object.

EDIT *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;
 }

解决方案

You need to implement the Parcelable interface

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

I think for the writing of the projectsList to work, the Project class also needs to implement Parcelable.

See e.g. this class for an example.