是否可以向Android上的SharedPreferences添加數(shù)組或?qū)ο笪矣幸粋€(gè)ArrayList具有名稱和圖標(biāo)指針的對(duì)象,我希望將其保存在SharedPreferences..我該怎么辦?注意:我不想使用數(shù)據(jù)庫
3 回答

阿晨1998
TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個(gè)贊
用戶偏好
共享偏好 不嚴(yán)格地保存“用戶首選項(xiàng)”,例如用戶選擇了什么鈴聲。如果您對(duì)為應(yīng)用程序創(chuàng)建用戶首選項(xiàng)感興趣,請(qǐng)參閱PreferenceActivity,它為您提供了一個(gè)活動(dòng)框架,用于創(chuàng)建用戶首選項(xiàng),用戶首選項(xiàng)將自動(dòng)持久化(使用共享首選項(xiàng))。
// 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 關(guān)注
- 739 瀏覽