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

后端 未结 5 1878
终归单人心
终归单人心 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: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 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 entry : prefs.getAll().entrySet() ) 
      backUpCurency_values.put( entry.getKey(), entry.getValue().toString() );
    

提交回复
热议问题