How to Store Hashmap So That It Can Be Retained It Value After a Device Reboot

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

Thanks very much but same thing can be done using the shared Preferences technique.
Below is the code to add data into shared preferences and check if already exists.

  SharedPreferences preferences = getSharedPreferences(
PREF_FILE_NAME, MODE_PRIVATE);
if (value.equals("")) {

boolean storedPreference = preferences.contains(key);
if (storedPreference) {
SharedPreferences.Editor editor = preferences.edit();
editor.remove(key); // value to store
Log.d("KEY",key);
editor.commit();
}
}else{

SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value); // value to store
Log.d("KEY",key);
editor.commit();
}

then we can access using the

SharedPreferences preferences = getSharedPreferences(
PREF_FILE_NAME, MODE_PRIVATE);
Map<String, String> map = (Map<String, String>) preferences.getAll();
if(!map.isEmpty()){
Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry pairs = (Map.Entry)iterator.next();
pairs.getKey()+pairs.getValue();
//write code here
}
}

Retrieve HashMap after restart of device

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

How to store HashMap on Android?

SharedPreferences also store data in key-value pair as hashmap, so why not get all key-values from hashmap and store into map, as it:

SharedPreferences pref= getContext().getSharedPreferences("Your_Shared_Prefs"), 
Context.MODE_PRIVATE);
SharedPreferences.Editor editor= pref.edit();

for (String s : map.keySet()) {
editor.putString(s, map.get(s));
}

To fetch values you can use:

public abstract Map<String, ?> getAll ()

http://developer.android.com/reference/android/content/SharedPreferences.html#getAll%28%29

use:

SharedPreferences pref= getContext().getSharedPreferences("Your_Shared_Prefs"), 
Context.MODE_PRIVATE);
HashMap<String, String> map= HashMap<String, String> pref.getAll();
for (String s : map.keySet()) {
String value=map.get(s);
//Use Value
}

Code is not compiled, so it may have some minor errors, but should work.

how to store and retrieve (key,value) kind of data using saved preferences android

You should just use a for-each loop and iterate through the map like this:

SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, 0).edit();

for( Entry<String, String> entry : backUpCurency_values.entrySet() )
editor.putString( entry.getKey(), entry.getValue() );

editor.commit();

Then when you need to get your values back for later use you do the following (provided that this SharedPreference-object is reserved for currency):

SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);

for( Entry<String, ?> entry : prefs.getAll().entrySet() )
backUpCurency_values.put( entry.getKey(), entry.getValue().toString() );

Data in a HashMap after closing an android application

You can use serialization. It will write your data in a file on your hard drive and you will be able to load them when you relaunch the app.

Since you are using String as a key and as a value and since String implements Serializable it should be easy.

Here's how to write :

    File file = new File("nameOfYourFile");
FileOutputStream f = new FileOutputStream(file);
ObjectOutputStream s = new ObjectOutputStream(f);
s.writeObject(yourHashMap);
s.close();

And to read :

    File file = new File("temp");
FileInputStream f = new FileInputStream(file);
ObjectInputStream s = new ObjectInputStream(f);
HashMap<String, Object> fileObj2 = (HashMap<String, Object>) s.readObject();
s.close();

Changing value after it's placed in HashMap changes what's inside HashMap?

What's happening here is that you're placing a pointer to a list in the hashmap, not the list itself.

When you define

List<SomeType> list;

you're defining a pointer to a list, not a list itself.

When you do

map.put(somekey, list);

you're just storing a copy of the pointer, not the list.

If, somewhere else, you follow that pointer and modify the object at its end, anyone holding that pointer will still be referencing the same, modified object.

Please see http://javadude.com/articles/passbyvalue.htm for details on pass-by-value in Java.



Related Topics



Leave a reply



Submit