我无法通过拆分到另一个活动来获取其中包含另一个列表的列表对象。下面是我的代码。
SnipDataEvents.java
public class SnipDataEvents implements Parcelable {
private String title,image,language,bookingUrl,infoUrl,genre;
private List<Venues> venuesList;
public SnipDataEvents(String title,String image,String language,String genre,String bookingUrl,String infoUrl,List<Venues> list){
venuesList = new ArrayList<>();
venuesList.addAll(list);
this.title = title;
this.image = image;
this.language = language;
this.genre = genre;
this.bookingUrl = bookingUrl;
this.infoUrl = infoUrl;
this.venuesList = list;
}
protected SnipDataEvents(Parcel in) {
title = in.readString();
image = in.readString();
language = in.readString();
bookingUrl = in.readString();
infoUrl = in.readString();
genre = in.readString();
in.readTypedList(getVenuesList(),Venues.CREATOR);
}
public static final Creator<SnipDataEvents> CREATOR = new Creator<SnipDataEvents>() {
@Override
public SnipDataEvents createFromParcel(Parcel in) {
return new SnipDataEvents(in);
}
@Override
public SnipDataEvents[] newArray(int size) {
return new SnipDataEvents[size];
}
};
public String getImage() {
return image;
}
public String getTitle() {
return title;
}
public List<Venues> getVenuesList() {
return venuesList;
}
public String getBookingUrl() {
return bookingUrl;
}
public String getGenre() {
return genre;
}
public String getInfoUrl() {
return infoUrl;
}
public String getLanguage() {
return language;
}
public void setVenuesList(List<Venues> venuesList) {
this.venuesList = venuesList;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(title);
parcel.writeString(image);
parcel.writeString(language);
parcel.writeString(bookingUrl);
parcel.writeString(infoUrl);
parcel.writeString(genre);
parcel.writeTypedList(venuesList);
}
}
Venues.java
public class Venues implements Parcelable {
private String name;
//private List<String> showTimes;
public Venues(String name, List<String> showTimes) {
//showTimes = new ArrayList<>();
this.name = name;
//this.showTimes = showTimes;
}
protected Venues(Parcel in) {
name = in.readString();
//showTimes = in.createStringArrayList();
}
public static final Creator<Venues> CREATOR = new Creator<Venues>() {
@Override
public Venues createFromParcel(Parcel in) {
return new Venues(in);
}
@Override
public Venues[] newArray(int size) {
return new Venues[size];
}
};
public String getName() {
return name;
}
//public List<String> getShowTimes() {
// return showTimes;
// }
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(name);
}
}
MainActivity.java
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("snipdataevents",(ArrayList<SnipDataEvents>)snipDataEvents);
intent.putExtras(bundle);
startActivity(intent);
DetailActivity.java
Bundle bundle = getIntent().getExtras();
snipDataEvents = bundle.getParcelableArrayList("snipdataevents");
当我运行代码时,在DetailActivity.java bundle.getParcelableArrayList(“ snipdataevents”)中返回null
当我删除此
in.readTypedList(getVenuesList(),Venues.CREATOR);
和
parcel.writeTypedList(venuesList);
在SnipDataEvents.java中的行之上 然后运行,bundle.getParcelableArrayList(“ snipdataevents”)返回列表。
我不知道代码有什么问题... 谁能帮忙吗?
答案 0 :(得分:0)
您可以通过以字符串的形式传递ArrayList 来完成此操作。
在依赖项块 build.gradle 中添加implementation 'com.google.code.gson:gson:2.8.4'
。
单击同步项目和 Gradle 文件
public class SnipDataEvents {
private String title, image, language, bookingUrl, infoUrl, genre;
private List<Venues> venuesList;
public SnipDataEvents(String title, String image, String language, String genre, String bookingUrl, String infoUrl, List<Venues> list) {
venuesList = new ArrayList<>();
venuesList.addAll(list);
this.title = title;
this.image = image;
this.language = language;
this.genre = genre;
this.bookingUrl = bookingUrl;
this.infoUrl = infoUrl;
this.venuesList = list;
}
public String getImage() {
return image;
}
public String getTitle() {
return title;
}
public List<Venues> getVenuesList() {
return venuesList;
}
public void setVenuesList(List<Venues> venuesList) {
this.venuesList = venuesList;
}
public String getBookingUrl() {
return bookingUrl;
}
public String getGenre() {
return genre;
}
public String getInfoUrl() {
return infoUrl;
}
public String getLanguage() {
return language;
}
}
public class Venues {
private String name;
public Venues(String name, List<String> showTimes) {
this.name = name;
}
public String getName() {
return name;
}
}
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
Bundle bundle = new Bundle();
bundle.putExtra("snipdataevents", new Gson().toJson(snipDataEvents));
intent.putExtras(bundle);
startActivity(intent);
String json = getIntent().getStringExtra("snipdataevents");
snipDataEvents = Arrays.asList(new Gson().fromJson(json, SnipDataEvents[].class))