在 Android 中创建自定义 java 对象 Parcelable

发布于 2024-12-07 01:19:14 字数 2145 浏览 0 评论 0原文

我正在尝试通过意图将 MyCustomObject 类型的对象发送到另一个活动。我知道,要使类 Parcelable,我应该执行 public class MyCustomObject Implements Parcelable,但我不确定自定义对象数组如何在 Parcelable 中工作。这是我到目前为止所得到的。另外,我是否也需要使 Search 类实现 Parcelable ?

这是我更新的答案现在,当我执行intent.getParcelableExtra(search)时,我会得到一个空对象。也许我没有正确创建搜索数组?

public class MyCustomObject implements Parcelable{
    public Search search[];

    public MyCustomObject(Parcel in){
        in.readArray(MyCustomObject.class.getClassLoader());
    }

    @Override
    public int describeContents(){
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags){
        dest.writeArray(search)
    }

    public static final Parcelable.Creator<MyCustomObject> CREATOR = new Parcelable.Creator<MyCustomObject>(){
        @Override
        public MyCustomObject createFromParcel(Parcel source){
            return new MyCustomObject(source);
        }

        @Override
        public MyCustomObject[] newArray(int size){
            return new MyCustomObject[size];
        }
    }

    public static class Search implements Parcelable{
        public int rank;
        public String title;
        public String[] imageURL;

        public Search(Parcel in){
            rank = in.readInt();
            title = in.readString();

            String[] url = new String[4];
            in.readStringArray(url);
            url = imageURL
        }

        @Override
        public int describeContents(){
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags){
            dest.writeInt(rank);
            dest.writeString(title);
            dest.writeStringArray(imageURL);
        }

        public static final Parcelable.Creator<Search> CREATOR = new Parcelable.Creator<Search>(){

        @Override
        public Search createFromParcel(Parcel source){
            return new Search(source);
        }

        @Override
        public Search[] newArray(int size){
            return new Search[size];
        }
    }

    }
}

I am trying to send an object of type MyCustomObject to another activity via an intent. I know, to make the class Parcelable, I should do public class MyCustomObject implements Parcelable, but I am not sure how custom object arrays work in parcelable. Here's what I have got so far.. Also, do I need to make the Search class implements Parcelable too?

Here is my updated answer I now get a null object when I do intent.getParcelableExtra(search). Maybe I am not creating the search array properly?

public class MyCustomObject implements Parcelable{
    public Search search[];

    public MyCustomObject(Parcel in){
        in.readArray(MyCustomObject.class.getClassLoader());
    }

    @Override
    public int describeContents(){
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags){
        dest.writeArray(search)
    }

    public static final Parcelable.Creator<MyCustomObject> CREATOR = new Parcelable.Creator<MyCustomObject>(){
        @Override
        public MyCustomObject createFromParcel(Parcel source){
            return new MyCustomObject(source);
        }

        @Override
        public MyCustomObject[] newArray(int size){
            return new MyCustomObject[size];
        }
    }

    public static class Search implements Parcelable{
        public int rank;
        public String title;
        public String[] imageURL;

        public Search(Parcel in){
            rank = in.readInt();
            title = in.readString();

            String[] url = new String[4];
            in.readStringArray(url);
            url = imageURL
        }

        @Override
        public int describeContents(){
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags){
            dest.writeInt(rank);
            dest.writeString(title);
            dest.writeStringArray(imageURL);
        }

        public static final Parcelable.Creator<Search> CREATOR = new Parcelable.Creator<Search>(){

        @Override
        public Search createFromParcel(Parcel source){
            return new Search(source);
        }

        @Override
        public Search[] newArray(int size){
            return new Search[size];
        }
    }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦亿 2024-12-14 01:19:14

有几件事。首先,您的 Search 对象还需要实现 Parcelable。接下来,您需要创建一个名为 CREATOR 的静态字段,该字段实现 Parcelable.Creator。这将有一个名为 createFromParcel 的方法,该方法返回 MyCustomObject 的实例。您可以在此处从包中读取数据并创建 MyCustomObject 的新实例。这本质上与 writeToParcel 相反。这一切也适用于搜索,因为您必须使其实现 Parcelable。总结:

  • 让 Search 实现 Parcelable
  • 实现 Search.writeToParcel
  • 在 Search 中创建一个名为 CREATOR 的静态字段,该字段实现 Parcelable.Creator
  • 实现 Search.CREATOR 的 createFromParcel 方法
  • 在 MyCustomObject 中创建一个名为 CREATOR 的静态字段,该字段实现 Parcelable.Creator
  • 实现 MyCustomObject.CREATOR 的 createFromParcel 方法

A couple of things. First, your Search object will need to implement Parcelable as well. Next, you need to create a static field called CREATOR that implements Parcelable.Creator<MyCustomObject>. This will have a method called createFromParcel that returns an instance of MyCustomObject. That is where you read data out of a parcel and create a new instance of MyCustomObject. This is essentially the opposite of writeToParcel. This all applies to Search as well since you must make it implement Parcelable. In summary:

  • Make Search implement Parcelable
  • Implement Search.writeToParcel
  • Create a static field in Search called CREATOR that implements Parcelable.Creator<Search>
  • Implement the createFromParcel method of Search.CREATOR
  • Create a static field in MyCustomObject called CREATOR that implements Parcelable.Creator<MyCustomObject>
  • Implement the createFromParcel method of MyCustomObject.CREATOR
梅倚清风 2024-12-14 01:19:14

当您从 Parcel 读取Array 时,您想要读取 Search[] 数组。不是 MyCustomObject。

    public MyCustomObject(Parcel in){
       in.readArray(MyCustomObject.class.getClassLoader());
    }

when you readArray from Parcel, you want to read a Search[] array. Not the MyCustomObject.

    public MyCustomObject(Parcel in){
       in.readArray(MyCustomObject.class.getClassLoader());
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文