我有两个实现Parcelable的类。我必须将其中之一的对象写入列表。第一个模型:
public class Challenge implements Parcelable {
private long id;
@JsonProperty("max_volume")
private int maxVolume;
@JsonProperty("current_volume")
private int currentVolume;
private String name;
private String description;
@JsonProperty("max_count")
private int maxCount;
@JsonProperty("date_from")
private Date dateFrom;
@JsonProperty("date_to")
private Date dateTo;
@JsonProperty("create_time")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm ZZZZ")
private Date createTime;
private ChallengeStatus status;
@JsonProperty("update_date")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm ZZZZ")
private Date updateTime;
private ChallengeCategory category;
private Subject subject;
private ArrayList<User> userArrayList;
private int practice;
public double getProgress() {
return progress;
}
public void setProgress(double progress) {
this.progress = progress;
}
public int getPractice() {
return practice;
}
public void setPractice(int practice) {
this.practice = practice;
}
private double progress;
public Challenge() {
}
private Creator<User> creator;
protected Challenge(Parcel in) {
id = in.readLong();
maxVolume = in.readInt();
currentVolume = in.readInt();
name = in.readString();
description = in.readString();
maxCount = in.readInt();
// dateFrom = new Date(in.readLong());
// dateTo = new Date(in.readLong());
// createTime = new Date(in.readLong());
// status = ChallengeStatus.getEnum(in.readString());
// updateTime = new Date(in.readLong());
// category = ChallengeCategory.getEnum(in.readString());
subject = in.readParcelable(Subject.class.getClassLoader());
userArrayList = in.readTypedList(userArrayList, User.CREATOR);
}
public static final Creator<Challenge> CREATOR = new Creator<Challenge>() {
@Override
public Challenge createFromParcel(Parcel in) {
return new Challenge(in);
}
@Override
public Challenge[] newArray(int size) {
return new Challenge[size];
}
};
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getMaxVolume() {
return maxVolume;
}
public void setMaxVolume(int maxVolume) {
this.maxVolume = maxVolume;
}
public int getCurrentVolume() {
return currentVolume;
}
public void setCurrentVolume(int currentVolume) {
this.currentVolume = currentVolume;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getMaxCount() {
return maxCount;
}
public void setMaxCount(int maxCount) {
this.maxCount = maxCount;
}
public Date getDateFrom() {
return dateFrom;
}
public void setDateFrom(Date dateFrom) {
this.dateFrom = dateFrom;
}
public Date getDateTo() {
return dateTo;
}
public void setDateTo(Date dateTo) {
this.dateTo = dateTo;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public ChallengeStatus getStatus() {
return status;
}
public void setStatus(ChallengeStatus status) {
this.status = status;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public ChallengeCategory getCategory() {
return category;
}
public void setCategory(ChallengeCategory category) {
this.category = category;
}
public Subject getSubject() {
return subject;
}
public void setSubject(Subject subject) {
this.subject = subject;
}
@Override
public int describeContents() {
return 0;
}
public ArrayList<User> getUserArrayList() {
return userArrayList;
}
public void setUserArrayList(ArrayList<User> userArrayList) {
this.userArrayList = userArrayList;
}
public static Creator<Challenge> getCREATOR() {
return CREATOR;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(id);
dest.writeInt(maxVolume);
dest.writeInt(currentVolume);
dest.writeString(name);
dest.writeString(description);
dest.writeInt(maxCount);
// dest.writeLong(dateFrom.getTime());
// dest.writeLong(dateTo.getTime());
// dest.writeLong(createTime.getTime());
// dest.writeString(status.toString());
// dest.writeLong(updateTime.getTime());
// dest.writeString(category.toString());
dest.writeParcelable(subject, flags);
dest.writeTypedList(userArrayList);
}
}
我在这一行有一个问题:
userArrayList = in.readTypedList(userArrayList, User.CREATOR);
看起来像这样:还有第二个模型的示例:public class User implements Parcelable {
long id;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm ZZZZ")
Date time;
String image_src;
String first_name;
String last_name;
public User() {
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public String getImage_src() {
return image_src;
}
public void setImage_src(String image_src) {
this.image_src = image_src;
}
protected User(Parcel parcel) {
id = parcel.readLong();
time = new Date(parcel.readLong());
image_src = parcel.readString();
first_name = parcel.readString();
last_name = parcel.readString();
}
public static final Creator<User> CREATOR = new Creator<User>() {
@Override
public User createFromParcel(Parcel in) {
return new User(in);
}
@Override
public User[] newArray(int size) {
return new User[size];
}
};
public static Creator<User> getCREATOR() {
return CREATOR;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeLong(id);
// parcel.writeLong(time.getTime());
parcel.writeString(image_src);
parcel.writeString(first_name);
parcel.writeString(last_name);
}
}
你能帮我发现我的错误吗?
最佳答案
您必须先初始化userArrayList
才能使用它:
userArrayList = new ArrayList<User>();
in.readTypedList(userArrayList, User.CREATOR);