如何使我的自定义对象Parcelable?

Jea*_*nez 329 android parcelable

我正在尝试让我的对象Parcelable.但是,我有自定义对象,这些对象具有ArrayList我所做的其他自定义对象的属性.

最好的方法是什么?

fic*_*ion 426

你可以找到这样一些例子在这里,在这里(代码在这里拍摄),并在这里.

您可以为此创建一个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];
           }
       };
   }
Run Code Online (Sandbox Code Playgroud)

创建此类后,您可以轻松地通过此类传递Intent此类的对象,并在目标活动中恢复此对象.

intent.putExtra("student", new Student("1","Mike","6"));
Run Code Online (Sandbox Code Playgroud)

在这里,学生是您需要从捆绑中取消数据的密钥.

Bundle data = getIntent().getExtras();
Student student = (Student) data.getParcelable("student");
Run Code Online (Sandbox Code Playgroud)

此示例仅显示String类型.但是,您可以包含任何类型的数据.试试看.

编辑:另一个例子,由Rukmal Dias建议.

  • OP询问如何传递自定义对象而不是常规字符串.任何人都可以通过常规字符串.你能解释如何用CUSTOM OBJECT创建它吗? (14认同)

fra*_*ssb 187

这是一个从您创建的类创建Parcelable类的网站:

http://www.parcelabler.com/


naX*_*aXa 110

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

这些插件根据类中的字段生成Android Parcelable样板代码.

插件演示


iti*_*skj 55

1.进口 Android Parcelable code generator

在此输入图像描述

2.创建一个类

public class Sample {
    int id;
    String name;
}
Run Code Online (Sandbox Code Playgroud)

3.从菜单中生成> Parcelable

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

完成.

  • 对于那些不知道"浏览存储库"对话框所在位置的人:"文件">"设置...">"插件",然后单击"浏览存储库..."按钮. (7认同)

naX*_*aXa 21

怎么样?带注释.

您只需使用特殊注释注释POJO,其余的就是库.

警告!

我不确定Hrisey,Lombok和其他代码生成库是否与Android的新构建系统兼容.它们可能会或可能不会与热交换代码(即jRebel,Instant Run)很好地配合使用.

优点:

  • 代码生成库使您免受样板源代码的影响.
  • 注释使你的课堂变得美丽.

缺点:

  • 它适用于简单的类.制作复杂的类可能是棘手的.
  • 龙目岛和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 */
}
Run Code Online (Sandbox Code Playgroud)

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

Hrisey in Gradle依赖:

provided "pl.mg6.hrisey:hrisey:${hrisey.version}"
Run Code Online (Sandbox Code Playgroud)

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

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

在此输入图像描述
*不要一起启用Hrisey和Lombok插件,否则在IDE启动期间会出现错误.


Parceler

使用Parceler的Parcelable类:

@java.org.parceler.Parcel
public class POJOClass {
    /* Fields, accessors, default constructor */
}
Run Code Online (Sandbox Code Playgroud)

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

public static <T> Parcelable wrap(T input);
Run Code Online (Sandbox Code Playgroud)

要取消引用@Parcel,只需调用以下Parcels类的方法

public static <T> T unwrap(Parcelable input);
Run Code Online (Sandbox Code Playgroud)

Gradle依赖项中的Parceler:

compile "org.parceler:parceler-api:${parceler.version}"
provided "org.parceler:parceler:${parceler.version}"
Run Code Online (Sandbox Code Playgroud)

README中查找支持的属性类型.


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*/);
    }
}
Run Code Online (Sandbox Code Playgroud)

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}"
}
Run Code Online (Sandbox Code Playgroud)

PaperParcel

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

docs中的用法示例.
使用@PaperParcel,实现PaperParcelable和添加JVM静态实例来注释您的数据类,PaperParcelable.Creator例如:

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

对于Kotlin用户,请参阅Kotlin用法 ; 对于AutoValue用户,请参阅AutoValue Usage.


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

}
Run Code Online (Sandbox Code Playgroud)

Android的易插件协助与注解处理器的工作结合Android的工作室.


Rjz*_*ara 7

我找到了创建Parcelable类的最简单方法

在此输入图像描述


Nag*_*hal 6

在 Android Studio 中创建没有插件的 Parcelable 类

在您的类中实现 Parcelable,然后将光标放在“实现 Parcelable”上并点击 Alt+Enter并选择Add Parcelable implementation(见图)。就是这样。

在此处输入图片说明