java 如何在包中传递列表<>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28190847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to passing list<> in bundle
提问by user3016825
I have the following code:
我有以下代码:
List<ValueActivity> list = new ArrayList<ValueActivity>();
list = setList();
Intent intent = new Intent(NOTIFICATION);
Bundle bundle = new Bundle();
bundle.put ????("list", list);
intent.putExtra("bundle", bundle);
sendBroadcast(intent);
How to write line 5 and how getExtra in destination intent
如何编写第 5 行以及如何在目标意图中使用 getExtra
回答by David Wasser
You will need to have ValueActivity
implement Parcelable
interface and you will need to implement writeToParcel()
the CREATOR
and a suitable constructor that takes a Parcel
as argument. See the documentation of Parcelable
interface.
你需要有ValueActivity
实现Parcelable
的界面,你将需要实现writeToParcel()
的CREATOR
和合适的构造函数,一个Parcel
作为参数。请参阅Parcelable
接口文档。
To put the list into the Bundle
, use:
要将列表放入Bundle
,请使用:
bundle.putParcelableArrayList("list", list);
To get the list out of the Bundle
in the target activity, use:
要从Bundle
目标活动中获取列表,请使用:
List<ValueActivity> = listbundle.getParcelableArrayList("list");
回答by KOTIOS
try to use :
尝试使用:
String jsonList = gson.toJson(youList);
// Add String to bundle