how to passing list<> in bundle

2020-03-02 02:23发布

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

2条回答
ら.Afraid
2楼-- · 2020-03-02 02:49

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.

To put the list into the Bundle, use:

bundle.putParcelableArrayList("list", list);

To get the list out of the Bundle in the target activity, use:

List<ValueActivity> = listbundle.getParcelableArrayList("list");
查看更多
Bombasti
3楼-- · 2020-03-02 02:51

try to use :

String jsonList = gson.toJson(youList);
// Add String to bundle
查看更多
登录 后发表回答