我试着让我的对象可打包。但是,我有自定义对象,这些对象具有我所做的其他自定义对象的ArrayList属性。
最好的方法是什么?
我试着让我的对象可打包。但是,我有自定义对象,这些对象具有我所做的其他自定义对象的ArrayList属性。
最好的方法是什么?
当前回答
现在,您可以使用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"));
其他回答
你可以在这里、这里(代码在这里)和这里找到一些例子。
您可以为此创建一个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];
}
};}
在Android Studio中创建没有插件的Parcelable类
在你的类中实现Parcelable,然后把光标放在“implements Parcelable”上,然后按Alt+Enter并选择添加Parcelable实现(见图片)。就是这样。
现在,您可以使用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"));
IntelliJ IDEA和Android Studio有这样的插件:
★Android Parcelable代码生成器(Apache License 2.0) 汽车包裹(麻省理工学院许可证) SerializableParcelable Generator (MIT许可证) 可打包代码生成器(适用于Kotlin) (Apache许可证2.0)
这些插件基于类中的字段生成Android Parcelable样板代码。