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

后端 未结 5 1850
终归单人心
终归单人心 2021-01-06 18:10

I have a hash map table as below,

HashMap backUpCurency_values = new HashMap();

and i want to

相关标签:
5条回答
  • 2021-01-06 18:40

    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;
       }
    
    0 讨论(0)
  • 2021-01-06 18:43

    You can use SharedPreference like this:

    SharedPreferences s_pref=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    Editor edit=s_pref.edit();
    
    edit.putString("key","value");
    edit.commit();
    

    later you can use it like:

    String s=s_pref.getString("key","default value");
    

    But,you must have list of keys you have saved values with,into SharedPreferences so that you can get values easily at the time of retrieving them.

    0 讨论(0)
  • 2021-01-06 18:47

    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>);
    
    0 讨论(0)
  • 2021-01-06 18:49

    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() );
    
    0 讨论(0)
  • 2021-01-06 18:49

    To store the values use this code

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

    To retrieve the values use this code

    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
            }
        }
    
    0 讨论(0)
提交回复
热议问题