0

I have a HashMap with Integer+Object pairs, but once the device is powered off and on, the list is gone and I need to be able to retrieve the data after, otherwise my HashMap is pointless. I've tried Bundles, extending Serializable with the HashMap and the object that I'm putting in. Does anyone have any idea how to do this? I'm thinking the best way is the ObjectOutputStream but I just can't work out how to do it. Can anyone direct me to a tutorial/example of this or could give me a quick one?

Much Appreciated

user1281053
  • 387
  • 1
  • 5
  • 10

5 Answers5

4

once the device is powered off and on, the list is gone

List is also gone when you exit or restart Application. Create a Database for that.

See here

Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
2

Java objects - even those which can be serialized - are only alive as long as your process is alive, unless you take steps to actually serialize them and store your data persistently. There are several options for this, including SQLite databases or SharedPreferences. Read this guide.

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
1

You can save your data to SharedPreferances with this code:

    Map<Integer, Object> aMap = new HashMap<Integer, Object>();
    aMap.put(String.valueOf(1), object1);
    aMap.put(String.valueOf(2), object2);
    aMap.put(String.valueOf(3), object3);

    SharedPreferences keyValues = getSharedPreferences("Your_Shared_Prefs", Context.MODE_PRIVATE);
    SharedPreferences.Editor keyValuesEditor = keyValues.edit();

    for (Integer s : aMap.keySet()) {
        keyValuesEditor.putString(String.valueOf(s), toString(aMap.get(s)));
    }
    keyValuesEditor.commit();

where toString() method are taken from there: How to serialize an object into a string

Then after reopening application call smth like this:

    SharedPreferences keyValues = getSharedPreferences("Your_Shared_Prefs", Context.MODE_PRIVATE);
    Map<Integer, Object> map = new HashMap<Integer, Object>();
    for (String key : keyValues.getAll().keySet()) {
        map.put(Integer.valueOf(key), fromString(keyValues.getString(key, null)));
    }
Community
  • 1
  • 1
amukhachov
  • 5,822
  • 1
  • 41
  • 60
0

For storing data to the phone a content provider is your best option. They are not the easiest to start with. But there is a sample Notes application provided in android that implements a simple content provider.

Check out http://developer.android.com/guide/topics/providers/content-providers.html

But more importantly this http://developer.android.com/guide/topics/providers/content-provider-creating.html which has details on the notepad sample application

Or See This Question How to store hashmap so that it can be retained it value after a device reboot?

Community
  • 1
  • 1
Mark Hetherington
  • 1,612
  • 14
  • 24
0

As Samir Mangroliya said. All of your data in the Hashmap is gone when you restart the phone. Consider using SQLite to save the data. I would save the Integer as the Primary Key to each column in the database.

Tobias Moe Thorstensen
  • 8,861
  • 16
  • 75
  • 143