如何通过共享preferences的所有键循环?preferences

2023-09-12 21:44:44 作者:向来悲伤多过嬉笑

共享preferences有方法GETALL,但它没有返回条目尽管一些项存在:

SharedPreferences have method getAll, but it returns no entries despite the fact some keys exist:

PreferenceManager.getDefaultSharedPreferences(this).contains("addNewAddress");

返回true

returns true

Map<String, ?> keys=PreferenceManager.getDefaultSharedPreferences(this).getAll();

返回空图

什么是错的?如何让所有的共享preferences列表?

What is wrong? How to get list of all shared preferences?

推荐答案

你可以做的就是使用getAll()对共享preferences方法并获得所有值地图&LT;字符串,&GT; 然后你就可以轻松遍历。

What you can do is use getAll() method of SharedPreferences and get all the values in Map<String,?> and then you can easily iterate through.

Map<String,?> keys = prefs.getAll();

for(Map.Entry<String,?> entry : keys.entrySet()){
            Log.d("map values",entry.getKey() + ": " + 
                                   entry.getValue().toString());            
 }

欲了解更多,您可以检查$p$pfUtil.java's 转储()的实施。

 
精彩推荐
图片推荐