【问题标题】:how to send ArrayList of objects with intent [duplicate]如何发送有意图的对象的ArrayList [重复]
【发布时间】:2018-01-28 08:38:15
【问题描述】:

我如何发送和接收有意图的 ArrayList?例如:

ArrayList<model> m=new ArrayList<model>();

ArrayList 包含字符串布尔值和...

我试过这个:

intent.putParcelableArrayListExtra(DATA,(ArrayList<? extends Parcelable>) m)

【问题讨论】:

    标签: android android-intent arraylist


    【解决方案1】:

    假设ArrayList<Model>

    要使用putParcelableArrayListEtra(),您需要您的Model 类来实现Parcelable 接口。这涉及到相当多的样板代码,但是,您可以使用 plugin for Android Studio 来自动生成该代码。

    另一种选择是让Model 类实现Serializable 并使用putSerializable()

    【讨论】:

    • 感谢您的帮助。
    【解决方案2】:

    将arraylist放入Bundle喜欢:

    ArrayList<String> images=new ArrayList<>();    
    Bundle bundle = new Bundle();
    bundle.putSerializable("images", (Serializable) images);
    

    然后再放入intent extra。

    【讨论】:

      【解决方案3】:

      要通过Intent 发送任何对象,对象应实现SerializableParcelable。你的model 类应该实现SerializableParcelable

      要通过Intent 发送ArrayList ArrayList 内的所有对象应实现 SerializableParcelable 查看ParcelableSerialization 教程。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-03
        • 2019-05-21
        • 2016-11-27
        • 2014-05-10
        • 2014-02-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多