我希望存儲一個ArrayList,其中包含SharedPreferences中的Hashmap。我怎樣才能做到這一點?如何在SharedPreferences中存儲ArrayList <HashMap <String,String >>?
2
A
回答
4
您可以將您的收藏集轉換爲json並將其存儲爲共享首選項。無論何時您需要獲取數據,只需獲取字符串並將JSON轉換回您的集合。
//converting the collection into a JSON
JSONArray result= new JSONArray(collection);
SharedPreferences pref = getApplicationContext().getSharedPreferences(PREF_NAME, 0);
//Storing the string in pref file
SharedPreferences.Editor prefEditor = pref.edit();
prefEditor.putString(KEY, result.toString());
prefEditor.commit();
//Getting the JSON from pref
String storedCollection = pref.getString(KEY, null);
//Parse the string to populate your collection.
ArrayList<HashMap<String, String>> collection = new ArrayList<HashMap<String, String>>();
try {
JSONArray array = new JSONArray(storedCollection);
HashMap<String, String> item = null;
for(int i =0; i<array.length(); i++){
String obj = (String) array.get(i);
JSONObject ary = new JSONObject(obj);
Iterator<String> it = ary.keys();
item = new HashMap<String, String>();
while(it.hasNext()){
String key = it.next();
item.put(key, (String)ary.get(key));
}
collection.add(item);
}
} catch (JSONException e) {
Log.e(TAG, "while parsing", e);
}
的JSON應該是這個樣子:
[
"{test13=yeah3, test12=yeah2, test11=yeah1}",
"{test23=yeah3, test22=yeah2, test21=yeah1}",
"{test32=yeah2, test31=yeah1, test33=yeah3}"
]
+0
如何將它轉換回ArrayList? – 2013-03-26 14:27:31
+0
編輯我的答案來解析JSON到集合。另外,如果你想減少麻煩,你可以使用GSON – Atrix1987 2013-03-27 11:16:39
相關問題
- 1. 如何在SharedPreferences中存儲ArrayList <HashMap <String,String >>?
- 2. java:在ArrayList中存儲HashMap <String,String><Object>
- 3. 如何聲明HashMap <String,ArrayList <ArrayList <String> >>?
- 4. Android ArrayList <HashMap <String,String >>
- 5. 構造函數ArrayAdapter <ArrayList <HashMap <String,String >>>(Context,int,ArrayList <HashMap <String,String >>)未定義
- 6. 如何將HashMap <String,ArrayList <String>>存儲在列表中?
- 7. 從ArrayList中獲取元素<HashMap <String,HashMap <String,String >>>
- 8. 如何在AlertDialog中設置ArrayList <HashMap <String,String >>?
- 9. 將ArrayList <HashMap <String,String >>附加到另一個ArrayList <HashMap <String,String >>
- 10. 如何存儲/保存HashMap <String,列表<String>>
- 11. 如何將HashMap <Integer,ArrayList <String>>存儲到SQLite?
- 12. 如何從ArrayList中獲取對象<HashMap <String,String >>
- 13. 如何正確聯合HashMap <String,ArrayList <Object>>和HashMap <String,Object>
- 14. Map <String,Map <String,Boolean >> myMap = new HashMap <String,HashMap <String,Boolean >>();
- 15. Javapoet - TypeName - HashMap <String,HashMap <String,List <String> >> generation?
- 16. HashMap <String,ArrayList <Objects>>
- 17. set ArrayList <HashMap <String,Object>> value
- 18. Android:如何訂購ArrayList <HashMap <String,String >>?
- 19. 如何迭代Arraylist <HashMap <String,String >>?
- 20. 如何檢查值存在ArrayList <Hashmap <String,String >>在android中?
- 21. 在AsyncTask中傳遞ArrayList <HashMap <String,String >>
- 22. 在ArrayAdapter中設置ArrayList <HashMap <String,String >>
- 23. 加載數據到HashMap <String,HashMap <String,HashMap <String,ArrayList <ClassOb> >>>
- 24. 從ArrayList中刪除重複項<HashMap <String,String >>
- 25. 從ArrayList中取值<HashMap <String,String >>
- 26. 使用HashMap(String,ArrayList <String>)
- 27. 如何保存HashMap <String,String>?
- 28. 轉換ArrayList <ArrayList <String>>「返回」ArrayList <String>
- 29. 迭代HashMap <String,HashMap <String,Integer >>
- 30. 獲取ArrayList <HashMap <String,String >>的值用於顯示
你嘗試過這個http://sudarmuthu.com/blog/using-preference-api-in-android-applications – 2013-03-26 04:30:10
@Johnathan金請不要這樣做。序列化數組很好,但把哈希映射到它裏面.. – minhaz 2013-03-26 04:30:36