是否可以向Android上的SharedPreferences添加數組或對象我有一個ArrayList具有名稱和圖標指針的對象,我希望將其保存在SharedPreferences..我該怎么辦?注意:我不想使用數據庫
3 回答
阿晨1998
TA貢獻2037條經驗 獲得超6個贊
用戶偏好
共享偏好 不嚴格地保存“用戶首選項”,例如用戶選擇了什么鈴聲。如果您對為應用程序創建用戶首選項感興趣,請參閱PreferenceActivity,它為您提供了一個活動框架,用于創建用戶首選項,用戶首選項將自動持久化(使用共享首選項)。
// my list of names, icon locationsMap<String, String> nameIcons = new HashMap<String, String>();nameIcons.put("Noel", "/location/to/noel/icon.png");nameIcons.put("Bob", "another/location/to/bob/icon.png");nameIcons.put("another name", "last/location/icon.png");SharedPreferences keyValues = getContext().getSharedPreferences("name_icons_list", Context.MODE_PRIVATE);SharedPreferences.Editor keyValuesEditor = keyValues.edit();for (String s : nameIcons.keySet()) {
// use the name as the key, and the icon as the value
keyValuesEditor.putString(s, nameIcons.get(s));}keyValuesEditor.commit()最新情況:
- 3 回答
- 0 關注
- 745 瀏覽
添加回答
舉報
0/150
提交
取消
