簡體   English   中英

共享首選項中的android數組

[英]android array from shared preferences

我試圖遍歷共享首選項的集合,並生成HashMaps的ArrayList,但是有問題。

SharedPreferences settings = getSharedPreferences(pref, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("key1", "value1");
editor.putString("key2", "value2");

然后我在想一些類似的事情:

final ArrayList<HashMap<String,String>> LIST = new ArrayList<HashMap<String,String>>();
SharedPreferences settings = getSharedPreferences(pref, 0);
Map<String, ?> items = settings.getAll();
for(String s : items.keySet()){
    HashMap<String,String> temp = new HashMap<String,String>();
    temp.put("key", s);
    temp.put("value", items.get(s));
    LIST.add(temp);
}

這給出了以下錯誤:

The method put(String, String) in the type HashMap<String,String> is not applicable for the arguments (String, capture#5-of ?)

有一個更好的方法嗎?

哈奇有個正確的主意。 對象不是字符串,因此.toString()是必需的。

final ArrayList<HashMap<String,String>> LIST = new ArrayList<HashMap<String,String>>();
SharedPreferences settings = getSharedPreferences(pref, 0);
Map<String, ?> items = settings.getAll();
for(String s : items.keySet()){
    HashMap<String,String> temp = new HashMap<String,String>();
    temp.put("key", s);
    temp.put("value", items.get(s).toString());
    LIST.add(temp);
}

更改

 HashMap<String,String> temp = new HashMap<String,String>();
 final ArrayList<HashMap<String,String>> LIST = new ArrayList<HashMap<String,String>>();

 HashMap<String,?> temp = new HashMap<String,?>();
 final ArrayList<HashMap<String,?>> LIST = new ArrayList<HashMap<String,?>>();

它應該工作。 您不是放置字符串而是一個會導致錯誤的對象

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM