如何在ArrayList
之间传递ArrayList<ArrayList<CustomObject>>
类型的activites
?我已经使用Parcelable
模型类实现了customObject
。当我像intent.putParcelableArrayListExtra("catVideoList", categoriesVideoList)
那样传递时,它会给出错误。我知道如何将ArrayList
传递给ArrayList<CustomObject>
。
以下是我的模特课程:
public class VideoContentModel implements Parcelable {
String videoId;
String videoUrl;
String videoTitle;
String videoDescription;
String videoThumbnailUrl;
String videoDuration;
String videoBitrate;
String videoPublishDate;
String videoCreateDate;
String videoUpdateDate;
String videoChannelId;
String videoChannelName;
String videoFeedType;
public VideoContentModel(String videoId, String videoUrl, String videoTitle,
String videoThumbnailUrl, String videoDuration,
String videoBitrate, String videoPublishDate, String videoCreateDate,
String videoUpdateDate, String videoChannelId, String videoChannelName,
String videoFeedType, String videoDescription)
{
this.videoId = videoId;
this.videoUrl = videoUrl;
this.videoTitle = videoTitle;
this.videoThumbnailUrl = videoThumbnailUrl;
this.videoDuration = videoDuration;
this.videoBitrate = videoBitrate;
this.videoPublishDate = videoPublishDate;
this.videoCreateDate = videoCreateDate;
this.videoUpdateDate = videoUpdateDate;
this.videoChannelId = videoChannelId;
this.videoChannelName = videoChannelName;
this.videoFeedType = videoFeedType;
this.videoDescription = videoDescription;
}
public String getVideoId() {
return videoId;
}
public String getVideoUrl() {
return videoUrl;
}
public String getVideoTitle() {
return videoTitle;
}
public String getVideoDescription() {
return videoDescription;
}
public String getVideoThumbnailUrl() {
return videoThumbnailUrl;
}
public String getVideoDuration() {
return videoDuration;
}
public String getVideoBitrate() {
return videoBitrate;
}
public String getVideoPublishDate() {
return videoPublishDate;
}
public String getVideoCreateDate() {
return videoCreateDate;
}
public String getVideoUpdateDate() {
return videoUpdateDate;
}
public String getVideoChannelId() {
return videoChannelId;
}
public String getVideoChannelName() {
return videoChannelName;
}
public String getVideoFeedType() {
return videoFeedType;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.videoId);
dest.writeString(this.videoUrl);
dest.writeString(this.videoTitle);
dest.writeString(this.videoDescription);
dest.writeString(this.videoThumbnailUrl);
dest.writeString(this.videoDuration);
dest.writeString(this.videoBitrate);
dest.writeString(this.videoPublishDate);
dest.writeString(this.videoCreateDate);
dest.writeString(this.videoUpdateDate);
dest.writeString(this.videoChannelId);
dest.writeString(this.videoChannelName);
dest.writeString(this.videoFeedType);
}
protected VideoContentModel(Parcel in) {
this.videoId = in.readString();
this.videoUrl = in.readString();
this.videoTitle = in.readString();
this.videoDescription = in.readString();
this.videoThumbnailUrl = in.readString();
this.videoDuration = in.readString();
this.videoBitrate = in.readString();
this.videoPublishDate = in.readString();
this.videoCreateDate = in.readString();
this.videoUpdateDate = in.readString();
this.videoChannelId = in.readString();
this.videoChannelName = in.readString();
this.videoFeedType = in.readString();
}
public static final Parcelable.Creator<VideoContentModel> CREATOR = new Parcelable.Creator<VideoContentModel>() {
@Override
public VideoContentModel createFromParcel(Parcel source) {
return new VideoContentModel(source);
}
@Override
public VideoContentModel[] newArray(int size) {
return new VideoContentModel[size];
}
};
}
答案 0 :(得分:1)
这里不需要Parcelable,因为您使用的是简单对象(String)。 ArrayList实现了Serializable,你的CustomObject也是如此。 更改后,您可以使用
intent.putExtra("name", yourArrayList);
或
Bundle bundle = new Bundle();
bundle.putSerializable("name", yourArrayList);
Intent intent = new Intent(context, yourActivity.class);
intent.putExtras(bundle);
并在第二项活动中检索它。
答案 1 :(得分:1)
您可以使用Gson库,无需实现parcelable。
假设你的arraylist是:
ArrayList<ArrayList<CustomObject>> outer = new ArrayList<>();
填充后,您可以将其添加到意图中,如下所示:
Intent i = new Intent(Activity1.this, Activity2.class);
i.putExtra("randomdata", new Gson().toJson(outer));
您可以稍后在Activity2中检索此内容,如下所示:
String extra = getIntent().getStringExtra("randomdata");
ArrayList<ArrayList<CustomObject>> list = new Gson().fromJson(extra, new TypeToken<ArrayList<ArrayList<CustomObject>>>(){}.getType());
希望它有所帮助。