如何通过parcelable发送包含列表的列表对象
Posted
技术标签:
【中文标题】如何通过parcelable发送包含列表的列表对象【英文标题】:how to send list object with list in it through parcelable 【发布时间】:2018-12-15 15:34:05 【问题描述】:我无法通过可打包到另一个活动来获取其中包含另一个列表的列表对象。下面是我的代码。
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") 返回列表。
我不知道代码有什么问题... 有人可以帮忙吗??
【问题讨论】:
我不知道,但我建议 lib github.com/johncarl81/parceler.It's 易于使用 我不能使用第三方:( 【参考方案1】:您可以通过以String的形式传递ArrayList来完成此操作。
在 dependencies 块 build.gradle 中添加 implementation 'com.google.code.gson:gson:2.8.4'
。
点击 Sync Project with Gradle Files
SnipDataEvents.java
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;
Venues.java
public class Venues
private String name;
public Venues(String name, List<String> showTimes)
this.name = name;
public String getName()
return name;
MainActivity.java
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
Bundle bundle = new Bundle();
bundle.putExtra("snipdataevents", new Gson().toJson(snipDataEvents));
intent.putExtras(bundle);
startActivity(intent);
DetailActivity.java
String json = getIntent().getStringExtra("snipdataevents");
snipDataEvents = Arrays.asList(new Gson().fromJson(json, SnipDataEvents[].class))
【讨论】:
以上是关于如何通过parcelable发送包含列表的列表对象的主要内容,如果未能解决你的问题,请参考以下文章
Android 使用 setonclickfillinintent 发送 Parcelable
通过 Parcelable 发送嵌套 ArrayList 时出现 NullPointerException
将 Parcelable 对象的 ArrayList 保存为持久数据?
如何通过 Parcelable 接口将 WifiP2pManager 对象发送到另一个活动