1

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

K. Barresi
  • 1,275
  • 1
  • 21
  • 46
  • Explain what you are storing in a list of hashmaps. You could change it into an object where you can implement serializable , or if it's some type of cache move it to your application and make it static / singleton – Blundell Jun 15 '12 at 14:50
  • @Blundell: `ArrayList` is `Serializable`, right? Looks like he will be storing `String`s in `HashMap`s. – Bhesh Gurung Jun 15 '12 at 14:56

3 Answers3

2

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.

MrJre
  • 7,082
  • 7
  • 53
  • 66
1

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.

Community
  • 1
  • 1
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
1

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.

Addison
  • 177
  • 11
  • That would work, but I need the ArrayList to be editable. I'm populating the list from XML downloaded from a server, so it needs to be non static – K. Barresi Jun 15 '12 at 14:51
  • Alright, then I would look into extending Application*. Here is a good example: http://stackoverflow.com/questions/1944656/android-global-variable – Addison Jun 15 '12 at 14:56