Android的:如何通过的ArrayList<对象>从活动到另一个对象、ArrayList、Android、gt

2023-09-12 22:14:14 作者:不想听谎言

在ArrayList中的对象在外部库中定义。该对象包含了一些的int值,也是一个浮动[]。

The object in the arraylist is defined in an external library. The object contains some int values and also a float[].

我如何可以通过从活动到其他所有的ArrayList? 谢谢!

How can I pass all the ArrayList from an activity to another? Thanks!

顺便说一句,我没有对象类的控制。

By the way, I do not have the control of the object class.

推荐答案

这取决于对象的类型的数组列表中显示。如果你拥有控制权,并可以拥有它实现Parcelable,那么你可以使用 Intent.putParcelableArrayListExtra

It depends on the type of object that's in the array list. If you have control over it and can have it implement Parcelable, then you can use Intent.putParcelableArrayListExtra.

另一种方法是扩展应用程序和存储您的ArrayList那里。然后,它并不需要在Intent传递,并在应用程序的所有活动,可以通过调用访问 getApplication()。中的应用对象将在该应用程序的生命,如将存储在其中的任何数据。

Another approach is to extend Application and store your ArrayList there. It then doesn't need to be passed in the Intent, and all activities in the application can access it by calling getApplication(). The Application object will persist for the life of the application, as will any data stored in it.

编辑:

要使用应用程序对象的:

To use an Application object for this:

在编写类(姑且称之为所有MyApplication ),扩展 android.app.Application 。声明一个字段(姑且称之为阵列)来保存你的数组列表。 指定在清单中的&LT名称所有MyApplication ;用途&gt; 标记。 所有MyApplication 的实例将通过系统时,它会创建过程中为您的应用程序创建。 是希望访问该领域可以使用任何活动((MyApplication的)getApplication())。阵列。在第一个活动初始化它和在第二个检索。 Write a class (let's call it MyApplication) that extends android.app.Application. Declare a field (let's call it array) to hold your array list. Specify the name MyApplication in the manifest's <application> tag. An instance of MyApplication will be created by the system when it creates the process for your app. Any activity that wishes to access the field can use ((MyApplication) getApplication()).array. You initialize it in the first activity and retrieve it in the second.

另一种完全不同的方式是在一些类中声明的静态单。该文档实际上是在子类应用推荐本作更加模块化。这是一种黑客攻击,因为它基本上声明一个全局变量。但我想这是没有更多的黑客攻击的不是将随机数据在应用程序中的对象。

Another completely different approach is to declare a static singleton in some class. The docs actually recommend this over subclassing Application as being more modular. It's kind of a hack because it's basically declaring a global variable. But I suppose it's no more of a hack than putting random data in an Application object.