我想将一个类的数组列表从一个活动传递到另一个活动。要做到这一点,该类正在实现 Parcelable。问题是类中很少有字段为空。如何检查和仅发送不为空的值。我认为这可以通过 writeToParcel() 中的简单 if/else 轻松完成,但如何在将参数作为 Parcel 的类的构造函数中执行相同操作。如下
private Student(Parcel in) {
}
发送空值时存在一些问题..
编辑:添加代码
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(name);
dest.writeString(grade);
dest.writeString(dateOfBirth);
dest.writeString(place);
//Like wise there are many other fields
//We have to write only values which are not null
}
private Student(Parcel in) {
id=in.readString();
name=in.readString();
grade=in.readString();
dateOfBirth=in.readString();
place=in.readString();
//like wise have to read all other fields
//we have to make sure we read only values which are not null
}