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

12,139

Solution 1

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() );

Solution 2

You can use SharedPreferences.

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

//The below step you can repeat to put all the key value pair from the hashmap to the shared preference

editor.putString("Key", Value);

// Commit the edits!
editor.commit();

And to retrieve later use

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getString(<Key>, <defaultvalue>);

Solution 3

There are a few different ways to approach this. With nothing more to go on than the info that you want to store a HashMap to SharedPreferences, I can only make assumptions.

First thing I'd ask is whether you will be storing other things in the SharedPreferences as well- I'll assume you will.

Here is how I would approach it:

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("backUpCurency", stringify(backUpCurency_values));
    editor.commit();

You may want to see what stringify does:

   // turns a HashMap<String, String> into "key=value|key=value|key=value"
   private String stringify(HashMap<String, String> map) {
       StringBuilder sb = new StringBuilder();
       for (String key : map.keySet()) {
           sb.append(key).append("=").append(map.get(key)).append("|");
       }
       return sb.substring(0, sb.length() - 1); // this may be -2, but write a unit test
   }

Then you can just parse that string with known structure upon reading the shared preferences later.

   private HashMap<String, String> restoreIt() {
      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
      String backup = settings.getString("backUpCurency", "");
      HashMap<String, String> map = new HashMap<String, String>();
      for (String pairs : backup.split("|")) {
         String[] indiv = pairs.split("=");
         map.put(indiv[0], indiv[1]);
      }
      return map;
   }
Share:
12,139
vnshetty
Author by

vnshetty

Android application developer A good programmer is someone who always looks both ways before crossing a one-way street. ~Doug Linder It is amazing what you can accomplish if you do not care who gets the credit. -Harry S. Truman

Updated on August 02, 2022

Comments

  • vnshetty
    vnshetty almost 2 years

    I have a hash map table as below,

    HashMap<String, String> backUpCurency_values = new HashMap<String, String>();
    

    and i want to store these value for future use.. how can i do that?

    Edit:
    i will store to Country names and currencyvalue as key and value pair...