K. Barresi
K. Barresi

Reputation: 1315

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. In one activity, I store it like by adding a series of HashMap<String, String> items to the ArrayList.

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. Any suggestions?

Edit: I'm storing XML in the HashMap in the form of of tag, value

Upvotes: 1

Views: 1019

Answers (3)

Samir Mangroliya
Samir Mangroliya

Reputation: 40416

How would I go about accessing this in another activity?

First Way

  • Make it public static in first Activity and access in other Activity.

Second Way

and if you download xml and set data then use Bean Concept.

Upvotes: 1

Addison
Addison

Reputation: 177

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

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*. If you decide to do that, then you need to register the extend in the AndroidManifest.xml file as well.

Upvotes: 1

MrJre
MrJre

Reputation: 7161

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.

Upvotes: 2

Related Questions