如何使我的自定义对象可打包?

发布于 2024-12-01 06:35:52 字数 84 浏览 3 评论 0原文

我正在尝试使我的对象可打包。但是,我有自定义对象,并且这些对象具有我创建的其他自定义对象的 ArrayList 属性。

最好的方法是什么?

I'm trying to make my objects Parcelable. However, I have custom objects and those objects have ArrayList attributes of other custom objects I have made.

What would be the best way to do this?

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

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

发布评论

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

评论(11

夏九 2024-12-08 06:35:52

您可以在此处找到一些示例,此处(代码取自此处)此处

您可以为此创建一个 POJO 类,但您需要添加一些额外的代码以使其可Parcelable。看看实施情况。

public class Student implements Parcelable{
        private String id;
        private String name;
        private String grade;

        // Constructor
        public Student(String id, String name, String grade){
            this.id = id;
            this.name = name;
            this.grade = grade;
       }
       // Getter and setter methods
       .........
       .........

       // Parcelling part
       public Student(Parcel in){
           String[] data = new String[3];

           in.readStringArray(data);
           // the order needs to be the same as in writeToParcel() method
           this.id = data[0];
           this.name = data[1];
           this.grade = data[2];
       }

       @Оverride
       public int describeContents(){
           return 0;
       }

       @Override
       public void writeToParcel(Parcel dest, int flags) {
           dest.writeStringArray(new String[] {this.id,
                                               this.name,
                                               this.grade});
       }
       public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
           public Student createFromParcel(Parcel in) {
               return new Student(in); 
           }

           public Student[] newArray(int size) {
               return new Student[size];
           }
       };
   }

创建此类后,您可以像这样轻松地通过 Intent 传递此类的对象,并在目标 Activity 中恢复该对象。

intent.putExtra("student", new Student("1","Mike","6"));

在这里,student 是您从捆绑包中解包数据所需的密钥。

Bundle data = getIntent().getExtras();
Student student = (Student) data.getParcelable("student");

此示例仅显示 String 类型。但是,您可以打包任何您想要的数据。尝试一下。

编辑:另一个 示例,由 鲁克马尔·迪亚斯

You can find some examples of this here, here (code is taken here), and here.

You can create a POJO class for this, but you need to add some extra code to make it Parcelable. Have a look at the implementation.

public class Student implements Parcelable{
        private String id;
        private String name;
        private String grade;

        // Constructor
        public Student(String id, String name, String grade){
            this.id = id;
            this.name = name;
            this.grade = grade;
       }
       // Getter and setter methods
       .........
       .........

       // Parcelling part
       public Student(Parcel in){
           String[] data = new String[3];

           in.readStringArray(data);
           // the order needs to be the same as in writeToParcel() method
           this.id = data[0];
           this.name = data[1];
           this.grade = data[2];
       }

       @Оverride
       public int describeContents(){
           return 0;
       }

       @Override
       public void writeToParcel(Parcel dest, int flags) {
           dest.writeStringArray(new String[] {this.id,
                                               this.name,
                                               this.grade});
       }
       public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
           public Student createFromParcel(Parcel in) {
               return new Student(in); 
           }

           public Student[] newArray(int size) {
               return new Student[size];
           }
       };
   }

Once you have created this class, you can easily pass objects of this class through the Intent like this, and recover this object in the target activity.

intent.putExtra("student", new Student("1","Mike","6"));

Here, the student is the key which you would require to unparcel the data from the bundle.

Bundle data = getIntent().getExtras();
Student student = (Student) data.getParcelable("student");

This example shows only String types. But, you can parcel any kind of data you want. Try it out.

EDIT: Another example, suggested by Rukmal Dias.

爱你是孤单的心事 2024-12-08 06:35:52

IntelliJ IDEA 和 Android Studio 有用于此目的的插件:

这些插件生成 Android Parcelable 基于类中字段的样板代码。

插件演示

IntelliJ IDEA and Android Studio have plugins for this:

These plugins generate Android Parcelable boilerplate code based on fields in the class.

Plugin demo

被你宠の有点坏 2024-12-08 06:35:52

1. 导入 Android Parcelable 代码生成器

在此处输入图像描述

2. 创建类

public class Sample {
    int id;
    String name;
}

3. 生成 >可从菜单

在此处输入图像描述
在此处输入图像描述

完成。

1. Import Android Parcelable code generator

enter image description here

2. Create a class

public class Sample {
    int id;
    String name;
}

3. Generate > Parcelable from menu

enter image description here
enter image description here

Done.

︶葆Ⅱㄣ 2024-12-08 06:35:52

如何?带注释。

您只需使用特殊注释对 POJO 进行注释,库即可完成剩下的工作。

警告!

我不确定 Hrisey、Lombok 和其他代码生成库是否与 Android 的新构建系统兼容。它们可能会也可能不会与热插拔代码(即 jRebel、Instant Run)配合良好。

优点:

  • 代码生成库使您无需使用样板源代码。
  • 注释让你的课堂变得美丽。

缺点:

  • 它适用于简单的课程。使复杂的类可分割可能很棘手。
  • Lombok 和 AspectJ 不能很好地协同工作。 [详细信息]
  • 查看我的警告。

赫里西

警告!

Hrisey 在 Java 8 方面存在一个已知问题,因此目前无法用于 Android 开发。
请参阅#1 找不到符号错误 (JDK 8)

Hrisey 基于 Lombok。使用 Hrisey 的 Parcelable 类:

@hrisey.Parcelable
public final class POJOClass implements android.os.Parcelable {
    /* Fields, accessors, default constructor */
}

现在您不需要实现 Parcelable 接口的任何方法。 Hrisey 将在预处理阶段生成所有必需的代码。

Gradle 中的 Hrisey 依赖项:

provided "pl.mg6.hrisey:hrisey:${hrisey.version}"

请参阅此处了解支持的类型。 ArrayList 就是其中之一。

为您的 IDE 安装插件 - Hrisey xor Lombok*,并开始使用其惊人的功能!

输入图片此处描述
* 不要同时启用 Hrisey 和 Lombok 插件,否则您将在 IDE 启动期间收到错误消息。


Parceler

使用 Parceler 的 Parcelable 类:

@java.org.parceler.Parcel
public class POJOClass {
    /* Fields, accessors, default constructor */
}

要使用生成的代码,您可以直接引用生成的类,或者通过Parcels 实用程序类使用

public static <T> Parcelable wrap(T input);

要取消引用 @Parcel,只需在 Gradle 依赖项中调用 Parcels 类 Parceler 的以下

public static <T> T unwrap(Parcelable input);

compile "org.parceler:parceler-api:${parceler.version}"
provided "org.parceler:parceler:${parceler.version}"

方法 自述文件,了解受支持的属性类型


AutoParcel

AutoParcel 是一个 AutoValue 扩展,可生成 Parcelable 值。

只需将 implements Parcelable 添加到您的 @AutoValue 带注释的模型中:

@AutoValue
abstract class POJOClass implements Parcelable {
    /* Note that the class is abstract */
    /* Abstract fields, abstract accessors */

    static POJOClass create(/*abstract fields*/) {
        return new AutoValue_POJOClass(/*abstract fields*/);
    }
}

Gradle 构建文件中的 AutoParcel:

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

repositories {
    /*...*/
    maven {url "https://clojars.org/repo/"}
}

dependencies {
    apt "frankiesardo:auto-parcel:${autoparcel.version}"
}

PaperParcel

PaperParcel 是一个注释处理器,可以自动为 Kotlin 和 Java 生成类型安全的 Parcelable 样板代码。 PaperParcel 支持 Kotlin 数据类、通过 AutoValue 扩展的 Google AutoValue,或者仅支持常规 Java bean 对象。

来自文档的用法示例。
使用 @PaperParcel 注释您的数据类,实现 PaperParcelable,并添加 PaperParcelable.Creator 的 JVM 静态实例,例如:

@PaperParcel
public final class Example extends PaperParcelable {
    public static final PaperParcelable.Creator<Example> CREATOR = new PaperParcelable.Creator<>(Example.class);

    private final int test;

    public Example(int test) {
        this.test = test;
    }

    public int getTest() {
        return test;
    }
}

对于 Kotlin 用户,请参阅 < a href="https://github.com/grandstaish/paperparcel/wiki/Kotlin-Usage" rel="nofollow noreferrer">Kotlin 用法;对于 AutoValue 用户,请参阅 AutoValue 使用


ParcelableGenerator

ParcelableGenerator (README是中文写的,看不懂。贡献于此欢迎中英文开发者的回答)

来自 自述文件。

import com.baoyz.pg.Parcelable;

@Parcelable
public class User {

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

android-apt 插件有助于与 Android Studio 结合使用注释处理器。

How? With annotations.

You simply annotate a POJO with a special annotation and library does the rest.

Warning!

I'm not sure that Hrisey, Lombok, and other code generation libraries are compatible with Android's new build system. They may or may not play nicely with hot swapping code (i.e. jRebel, Instant Run).

Pros:

  • Code generation libraries save you from the boilerplate source code.
  • Annotations make your class beautiful.

Cons:

  • It works well for simple classes. Making a complex class parcelable may be tricky.
  • Lombok and AspectJ don't play well together. [details]
  • See my warnings.

Hrisey

Warning!

Hrisey has a known issue with Java 8 and therefore cannot be used for Android development nowadays.
See #1 Cannot find symbol errors (JDK 8).

Hrisey is based on Lombok. Parcelable class using Hrisey:

@hrisey.Parcelable
public final class POJOClass implements android.os.Parcelable {
    /* Fields, accessors, default constructor */
}

Now you don't need to implement any methods of Parcelable interface. Hrisey will generate all required code during preprocessing phase.

Hrisey in Gradle dependencies:

provided "pl.mg6.hrisey:hrisey:${hrisey.version}"

See here for supported types. The ArrayList is among them.

Install a plugin - Hrisey xor Lombok* - for your IDE and start using its amazing features!

enter image description here
* Don't enable Hrisey and Lombok plugins together or you'll get an error during IDE launch.


Parceler

Parcelable class using Parceler:

@java.org.parceler.Parcel
public class POJOClass {
    /* Fields, accessors, default constructor */
}

To use the generated code, you may reference the generated class directly, or via the Parcels utility class using

public static <T> Parcelable wrap(T input);

To dereference the @Parcel, just call the following method of Parcels class

public static <T> T unwrap(Parcelable input);

Parceler in Gradle dependencies:

compile "org.parceler:parceler-api:${parceler.version}"
provided "org.parceler:parceler:${parceler.version}"

Look in README for supported attribute types.


AutoParcel

AutoParcel is an AutoValue extension that enables Parcelable values generation.

Just add implements Parcelable to your @AutoValue annotated models:

@AutoValue
abstract class POJOClass implements Parcelable {
    /* Note that the class is abstract */
    /* Abstract fields, abstract accessors */

    static POJOClass create(/*abstract fields*/) {
        return new AutoValue_POJOClass(/*abstract fields*/);
    }
}

AutoParcel in Gradle build file:

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

repositories {
    /*...*/
    maven {url "https://clojars.org/repo/"}
}

dependencies {
    apt "frankiesardo:auto-parcel:${autoparcel.version}"
}

PaperParcel

PaperParcel is an annotation processor that automatically generates type-safe Parcelable boilerplate code for Kotlin and Java. PaperParcel supports Kotlin Data Classes, Google's AutoValue via an AutoValue Extension, or just regular Java bean objects.

Usage example from docs.
Annotate your data class with @PaperParcel, implement PaperParcelable, and add a JVM static instance of PaperParcelable.Creator e.g.:

@PaperParcel
public final class Example extends PaperParcelable {
    public static final PaperParcelable.Creator<Example> CREATOR = new PaperParcelable.Creator<>(Example.class);

    private final int test;

    public Example(int test) {
        this.test = test;
    }

    public int getTest() {
        return test;
    }
}

For Kotlin users, see Kotlin Usage; For AutoValue users, see AutoValue Usage.


ParcelableGenerator

ParcelableGenerator (README is written in Chinese and I don't understand it. Contributions to this answer from english-chinese speaking developers are welcome)

Usage example from README.

import com.baoyz.pg.Parcelable;

@Parcelable
public class User {

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

The android-apt plugin assists in working with annotation processors in combination with Android Studio.

夏日浅笑〃 2024-12-08 06:35:52

在 Android Studio 中创建不带插件的 Parcelable 类

在您的类中实现 Parcelable,然后将光标放在“implements Parcelable”上并按 Alt+Enter 并选择添加 Parcelable 实现< /代码>(见图)。就是这样。

输入图像描述这里

Create Parcelable class without plugin in Android Studio

implements Parcelable in your class and then put cursor on "implements Parcelable" and hit Alt+Enter and select Add Parcelable implementation (see image). that's it.

enter image description here

旧人哭 2024-12-08 06:35:52

我找到了创建 Parcelable 类

在此处输入图像描述

I have found simplest way to create Parcelable class

enter image description here

放赐 2024-12-08 06:35:52

这很简单,你可以使用android studio上的插件来制作对象Parcelables。

public class Persona implements Parcelable {
String nombre;
int edad;
Date fechaNacimiento;

public Persona(String nombre, int edad, Date fechaNacimiento) {
    this.nombre = nombre;
    this.edad = edad;
    this.fechaNacimiento = fechaNacimiento;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.nombre);
    dest.writeInt(this.edad);
    dest.writeLong(fechaNacimiento != null ? fechaNacimiento.getTime() : -1);
}

protected Persona(Parcel in) {
    this.nombre = in.readString();
    this.edad = in.readInt();
    long tmpFechaNacimiento = in.readLong();
    this.fechaNacimiento = tmpFechaNacimiento == -1 ? null : new Date(tmpFechaNacimiento);
}

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

    public Persona[] newArray(int size) {
        return new Persona[size];
    }
};}

It is very easy, you can use a plugin on android studio to make objects Parcelables.

public class Persona implements Parcelable {
String nombre;
int edad;
Date fechaNacimiento;

public Persona(String nombre, int edad, Date fechaNacimiento) {
    this.nombre = nombre;
    this.edad = edad;
    this.fechaNacimiento = fechaNacimiento;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.nombre);
    dest.writeInt(this.edad);
    dest.writeLong(fechaNacimiento != null ? fechaNacimiento.getTime() : -1);
}

protected Persona(Parcel in) {
    this.nombre = in.readString();
    this.edad = in.readInt();
    long tmpFechaNacimiento = in.readLong();
    this.fechaNacimiento = tmpFechaNacimiento == -1 ? null : new Date(tmpFechaNacimiento);
}

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

    public Persona[] newArray(int size) {
        return new Persona[size];
    }
};}
太阳公公是暖光 2024-12-08 06:35:52

我尝试使用Parcellable,时间很短,所以使用Gson如下。也许它可以帮助其他人......

PersonData personData;
BusinessData businessData;

以上都是模型对象。我希望它从 LoginActivity.class 传递到 MainActivity.class。 Parcellable 不适用于大型模型,并且需要时间进行更改。所以我这样做了,

startActivity(
    new Intent(LogInActivity.this, MainActivity.class)
    .putExtra("businessData", new Gson().toJson(businessData))
    .putExtra("personData", new Gson().toJson(personData))
);

在 MainActivity.class 中检索这些对象

BusinessData businessData = new Gson().fromJson(getIntent().getStringExtra("businessData"), BusinessData.class);
PresonData personData = new Gson().fromJson(getIntent().getStringExtra("personData"), PersonData.class);

I tried to use Parcellable, time was short, So used Gson as below. May be it could help few others...

PersonData personData;
BusinessData businessData;

The above both are model objects. I wanted it to pass to MainActivity.class from LoginActivity.class. Parcellable was not working with huge models and would take time to make changes..So I did this,

startActivity(
    new Intent(LogInActivity.this, MainActivity.class)
    .putExtra("businessData", new Gson().toJson(businessData))
    .putExtra("personData", new Gson().toJson(personData))
);

Retrieve these objects in MainActivity.class

BusinessData businessData = new Gson().fromJson(getIntent().getStringExtra("businessData"), BusinessData.class);
PresonData personData = new Gson().fromJson(getIntent().getStringExtra("personData"), PersonData.class);
虫児飞 2024-12-08 06:35:52

现在您可以使用 Parceler 库将您的任何自定义类转换为 Parcelable。只需使用 @Parcel 注释您的 POJO 类即可。
例如,

    @Parcel
    public class Example {
    String name;
    int id;

    public Example() {}

    public Example(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getName() { return name; }

    public int getId() { return id; }
}

您可以创建示例类的对象并包装包裹并通过意图作为捆绑包发送。例如

Bundle bundle = new Bundle();
bundle.putParcelable("example", Parcels.wrap(example));

现在要获取自定义类对象只需使用

Example example = Parcels.unwrap(getIntent().getParcelableExtra("example"));

Now you can use Parceler library to convert your any custom class in parcelable. Just annotate your POJO class with @Parcel.
e.g.

    @Parcel
    public class Example {
    String name;
    int id;

    public Example() {}

    public Example(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getName() { return name; }

    public int getId() { return id; }
}

you can create an object of Example class and wrap through Parcels and send as a bundle through intent. e.g

Bundle bundle = new Bundle();
bundle.putParcelable("example", Parcels.wrap(example));

Now to get Custom Class object just use

Example example = Parcels.unwrap(getIntent().getParcelableExtra("example"));
岁月染过的梦 2024-12-08 06:35:52

Android parcable 有一些独特的东西。这些内容如下:

  1. 您必须按照在包裹上放置数据的相同顺序来读取包裹。
  2. 从包裹中读取后,包裹将清空。也就是说,如果您的包裹有 3 个数据。然后读取3次后包裹就空了。

例子:
要创建 Parceble 类,它必须实现 Parceble。 Percable 有 2 个方法:

int describeContents();
void writeToParcel(Parcel var1, int var2);

假设您有一个 Person 类,它有 3 个字段,名字、姓氏和年龄。实现 Parceble 接口后。该接口如下所示:

import android.os.Parcel;
import android.os.Parcelable;
public class Person implements Parcelable{
    private String firstName;
    private String lastName;
    private int age;

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(firstName);
        parcel.writeString(lastName);
        parcel.writeInt(age);
    }

}

writeToParcel 方法中,我们按顺序在 Parcel 上写入/添加数据。之后,我们必须添加以下代码以从包中读取数据:

protected Person(Parcel in) {
        firstName = in.readString();
        lastName = in.readString();
        age = in.readInt();
    }

    public static final Creator<Person> CREATOR = new Creator<Person>() {
        @Override
        public Person createFromParcel(Parcel in) {
            return new Person(in);
        }

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

这里,Person 类正在获取包并在写入期间以相同的顺序获取数据。

现在,在意图getExtraputExtra期间给出如下代码:

放入额外

Person person=new Person();
                person.setFirstName("First");
                person.setLastName("Name");
                person.setAge(30);

                Intent intent = new Intent(getApplicationContext(), SECOND_ACTIVITY.class);
                intent.putExtra()
                startActivity(intent); 

获取额外:

Person person=getIntent().getParcelableExtra("person");

Full人员类别如下:

import android.os.Parcel;
import android.os.Parcelable;

public class Person implements Parcelable{
    private String firstName;
    private String lastName;
    private int age;



    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(firstName);
        parcel.writeString(lastName);
        parcel.writeInt(age);
    }

    protected Person(Parcel in) {
        firstName = in.readString();
        lastName = in.readString();
        age = in.readInt();
    }

    public static final Creator<Person> CREATOR = new Creator<Person>() {
        @Override
        public Person createFromParcel(Parcel in) {
            return new Person(in);
        }

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

}

Hope this will help you 
Thanks :)

Android parcable has some unique things. Those are given bellow:

  1. You have to read Parcel as the same order where you put data on parcel.
  2. Parcel will empty after read from parcel. That is if you have 3 data on your parcel. Then after read 3 times parcel will be empty.

Example:
To make a class Parceble it must be implement Parceble. Percable has 2 method:

int describeContents();
void writeToParcel(Parcel var1, int var2);

Suppose you have a Person class and it has 3 field, firstName,lastName and age. After implementing Parceble interface. this interface is given bellow:

import android.os.Parcel;
import android.os.Parcelable;
public class Person implements Parcelable{
    private String firstName;
    private String lastName;
    private int age;

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(firstName);
        parcel.writeString(lastName);
        parcel.writeInt(age);
    }

}

Here writeToParcel method we are writing/adding data on Parcel in an order. After this we have to add bellow code for reading data from parcel:

protected Person(Parcel in) {
        firstName = in.readString();
        lastName = in.readString();
        age = in.readInt();
    }

    public static final Creator<Person> CREATOR = new Creator<Person>() {
        @Override
        public Person createFromParcel(Parcel in) {
            return new Person(in);
        }

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

Here, Person class is taking a parcel and getting data in same an order during writing.

Now during intent getExtra and putExtra code is given bellow:

Put in Extra:

Person person=new Person();
                person.setFirstName("First");
                person.setLastName("Name");
                person.setAge(30);

                Intent intent = new Intent(getApplicationContext(), SECOND_ACTIVITY.class);
                intent.putExtra()
                startActivity(intent); 

Get Extra:

Person person=getIntent().getParcelableExtra("person");

Full Person class is given bellow:

import android.os.Parcel;
import android.os.Parcelable;

public class Person implements Parcelable{
    private String firstName;
    private String lastName;
    private int age;



    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(firstName);
        parcel.writeString(lastName);
        parcel.writeInt(age);
    }

    protected Person(Parcel in) {
        firstName = in.readString();
        lastName = in.readString();
        age = in.readInt();
    }

    public static final Creator<Person> CREATOR = new Creator<Person>() {
        @Override
        public Person createFromParcel(Parcel in) {
            return new Person(in);
        }

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

}

Hope this will help you 
Thanks :)
爱给你人给你 2024-12-08 06:35:52

放置:
bundle.putSerialized("key",(Serializing) object);

获取:
列表<对象> obj = (List)((Serialized)bundle.getSerialized("key"));

To put:
bundle.putSerializable("key",(Serializable) object);

To get:
List<Object> obj = (List<Object>)((Serializable)bundle.getSerializable("key"));

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