简体   繁体   English

如何存储ArrayList <HashMap<String, String> &gt;

[英]How to store ArrayList<HashMap<String, String>>

I'm trying to figure out the best way to store an ArrayList<HashMap<String, String>> item so that it's accessible across activities. 我正在试图找出存储ArrayList<HashMap<String, String>>项的最佳方法,以便可以跨活动访问它。 In one activity, I store it like by adding a series of HashMap<String, String> items to the ArrayList. 在一个活动中,我通过向ArrayList添加一系列HashMap<String, String>项来存储它。

How would I go about accessing this in another activity? 我将如何在另一项活动中访问此内容? I tried SharedPreferences, but it will only store it as a string, as far as I know. 我尝试过SharedPreferences,但据我所知,它只会将它存储为字符串。 Any suggestions? 有什么建议么?

Edit: I'm storing XML in the HashMap in the form of of tag, value 编辑:我正在以标签,值的形式将XML存储在HashMap中

Why don't you use the application class for this? 你为什么不使用这个应用程序类 That way you can put the data structure there, and its info can be shared across activities. 这样,您就可以将数据结构放在那里,并且可以跨活动共享其信息。 Just be aware that the application can be destroyed, and that keeping it in the application means that you'd have to save it in sharedprefs or write to disk if you want to keep it around permanently. 请注意,应用程序可以被销毁,并且将其保留在应用程序中意味着您必须将其保存在sharedprefs中,或者如果要永久保留它,则必须写入磁盘。

How would I go about accessing this in another activity? 我将如何在另一项活动中访问此内容?

First Way 第一道路

  • Make it public static in first Activity and access in other Activity. 在第一个Activity中将其设为public,在其他Activity中访问。

Second Way 第二种方式

and if you download xml and set data then use Bean Concept. 如果你下载xml并设置数据,那么使用Bean Concept.

The easiest way would be to create a separate class and populate it with static variables, as shown here: 最简单的方法是创建一个单独的类并用静态变量填充它,如下所示:

http://www.glenmccl.com/tip_002.htm http://www.glenmccl.com/tip_002.htm

That way you can create static methods to get and set values as needed across activities. 这样,您可以创建静态方法,以根据需要跨活动获取和设置值。

However, some would argue this is not as safe as simply creating a new class that extends Application*. 但是,有些人会认为这不像创建扩展Application *的新类那样安全。 If you decide to do that, then you need to register the extend in the AndroidManifest.xml file as well. 如果您决定这样做,那么您还需要在AndroidManifest.xml文件中注册extend。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM