如何存储和检索(键,值)使用节省preferences的Andr​​oid样的数据节省、数据、Andr、preferences

2023-09-05 10:21:28 作者:来我怀抱

我有如下一个哈希映射表,

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?

编辑:  我将存储国名和currencyvalue作为键值对...

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

推荐答案

您应该只使用for-each循环并遍历图是这样的:

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

然后,当你需要让你的价值观回来以后使用你做下面的(只要该共享preference对象是保留给货币):

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