【问题标题】:how to passing list<> in bundle如何在捆绑中传递列表<>
【发布时间】:2015-03-27 06:28:48
【问题描述】:

我有以下代码:

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);

如何编写第 5 行以及如何在目标意图中 getExtra

【问题讨论】:

标签: java android android-intent arraylist bundle


【解决方案1】:

您需要让ValueActivity 实现Parcelable 接口,并且您需要实现writeToParcel()CREATOR 和一个以Parcel 作为参数的合适的构造函数。参见Parcelable接口的文档。

要将列表放入Bundle,请使用:

bundle.putParcelableArrayList("list", list);

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

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

【讨论】:

    【解决方案2】:

    尝试使用:

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

    【讨论】:

    • 完美解决方案。
    猜你喜欢
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多