我试着让我的对象可打包。但是,我有自定义对象,这些对象具有我所做的其他自定义对象的ArrayList属性。

最好的方法是什么?


当前回答

你可以在这里、这里(代码在这里)和这里找到一些例子。

您可以为此创建一个POJO类,但是您需要添加一些额外的代码以使其可封装。看一下实现。

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传递这个类的对象,并在目标活动中恢复这个对象。

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

在这里,student是将数据从包中解包所需要的键。

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

这个例子只显示了String类型。但是,你可以打包任何你想要的数据。试试吧。

编辑:另一个例子,由Rukmal Dias提出。

其他回答

这很简单,你可以在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];
    }
};}

现在,您可以使用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; }
}

你可以创建一个Example类的对象,并通过包裹进行包装,并通过intent将其作为一个bundle发送。如

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

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

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

我尝试使用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);

IntelliJ IDEA和Android Studio有这样的插件:

★Android Parcelable代码生成器(Apache License 2.0) 汽车包裹(麻省理工学院许可证) SerializableParcelable Generator (MIT许可证) 可打包代码生成器(适用于Kotlin) (Apache许可证2.0)

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

怎么做?与注释。

您只需用一个特殊的注释注释POJO,库就会完成其余的工作。

警告! 我不确定Hrisey、Lombok和其他代码生成库是否与Android的新构建系统兼容。它们可能与热交换代码(如jRebel, Instant Run)很好地兼容,也可能不兼容。

优点:

代码生成库将您从样板源代码中拯救出来。 注释使你的类更漂亮。

缺点:

它适用于简单的类。使一个复杂的类可打包可能是棘手的。 Lombok和AspectJ不能很好地配合使用。(细节) 请看我的警告。


赫里塞

警告! Hrisey在Java 8中有一个已知的问题,因此目前不能用于Android开发。 参见#1无法找到符号错误(JDK 8)。

Hrisey以龙目岛为基地。Parcelable类使用Hrisey:

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

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

格拉德尔的赫里西:

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

请参见这里了解受支持的类型。数组列表就是其中之一。

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

*不要同时启用Hrisey和Lombok插件,否则在IDE启动时会出现错误。


Parceler

使用Parceler的Parcelable类:

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

要使用生成的代码,您可以直接引用生成的类,或者通过packages实用工具类使用

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

要解除对@Parcel的引用,只需调用packages类的下面方法

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

Gradle依赖中的打包器:

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

在README中查找受支持的属性类型。


自动包裹

AutoParcel是一个支持Parcelable值生成的AutoValue扩展。

只需添加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*/);
    }
}

AutoParcel在Gradle构建文件:

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通过AutoValue扩展,或者只是普通的Java bean对象。

文档中的用法示例。 用@PaperParcel注释数据类,实现PaperParcelable,并添加PaperParcelable的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用户,请参见Kotlin用法;对于AutoValue用户,请参见AutoValue用法。


ParcelableGenerator

ParcelableGenerator (README是用中文写的,我看不懂。欢迎以英语和汉语为母语的开发人员对这个答案做出贡献)

来自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结合使用注释处理器。