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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:10:22  来源:igfitidea点击:

how to passing list<> in bundle

javaandroidandroid-intentarraylistbundle

提问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 ValueActivityimplement Parcelableinterface and you will need to implement writeToParcel()the CREATORand a suitable constructor that takes a Parcelas argument. See the documentation of Parcelableinterface.

你需要有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 Bundlein the target activity, use:

要从Bundle目标活动中获取列表,请使用:

List<ValueActivity> = listbundle.getParcelableArrayList("list");

回答by KOTIOS

try to use :

尝试使用:

String jsonList = gson.toJson(youList);
// Add String to bundle