如何使用保存的首选项android存储和检索(键,值)类型的数据 [英] how to store and retrieve (key,value) kind of data using saved preferences android

查看:15
本文介绍了如何使用保存的首选项android存储和检索(键,值)类型的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?


我会将国家名称和货币值存储为键值对...


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

然后,当您需要取回您的值以供以后使用时,请执行以下操作(前提是此 SharedPreference-object 是为货币保留的):

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

这篇关于如何使用保存的首选项android存储和检索(键,值)类型的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆