将更多值附加到共享首选项而不是覆盖现有值 [英] Append more values to Shared Preferences rather than overwriting the existing values
问题描述
在我的应用程序中,我需要将值保存到 Sharedpreferences 文件 RKs_Data 中而不覆盖现有数据.每次,我在我的应用程序中单击是",我都需要存储在RKs_Data"中的所有值,而不仅仅是在文件中包含最新的名称"和电话号码".
In my app, I need the values to be saved in to Sharedpreferences file RKs_Data without overwriting the existing data. Every time, I click 'Yes' in my app, I require all the values stored in the 'RKs_Data' instead of just having the latest 'name' and 'phoneNo' in to the file.
- 是否可以通过 SharedPreferences 这样做?如果是,如何?
- 如果没有,我实施此操作的下一个更好的选择是什么?
例如,
- 当我第一次单击Brian"等联系人之一时,它会将姓名和电话都保存在 SharedPreferences 中Brian 和 99999299999 第一次保存共享首选项文件RKS_Data"
当我点击其他联系人说Monet"时,我的 RKs_Data 应该如下所示:
- When I first Click one of the contacts like 'Brian', it saves in SharedPreferences both name and phone as Brian and 99999299999 with the first save on the Shared Preferences file 'RKS_Data'
When I Click on other contact say 'Monet', my RKs_Data should appear like this:
- 布莱恩 99999299999
- 莫奈00010000000等等....
我搜索过,但到处都只提到保存它,但没有提到附加或对数据进行一些操作...
I searched for but everywhere it is only mentioned about saving it but nothing about appending or doing some manipulations with the data...
请指导...
代码片段如下:
-------
public class RKsContacts_Main extends ListActivity {
// private ListView listView;
private List<ContactBean> list = new ArrayList<ContactBean>();
SharedPreferences sp;
File Fav_Contacts_file;
String contact = null;
List<String> listOfFavoritePhrases = new ArrayList<String>();
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rks_contactslist_main);
ListView listview = getListView();
sp = getSharedPreferences("PACKAGE", Context.MODE_PRIVATE);
-------
--------
@Override
public void onClick(DialogInterface dialog, int which) {
String serialized = sp.getString("phrases", null);
listOfFavoritePhrases = new ArrayList<String>(Arrays.asList(TextUtils.split(serialized, ","))); // Line 141
listOfFavoritePhrases.add(name);
listOfFavoritePhrases.add(phoneNo);
SharedPreferences.Editor editor = sp.edit();
editor.putString("phrases",TextUtils.join(",", listOfFavoritePhrases));
editor.commit();
}
});
alert.show();
}
--------
LogCat 下面:
07-03 09:00:51.014: E/AndroidRuntime(9574): FATAL EXCEPTION: main
07-03 09:00:51.014: E/AndroidRuntime(9574): Process: com.example.rkscontacts_list, PID: 9574
07-03 09:00:51.014: E/AndroidRuntime(9574): java.lang.NullPointerException
07-03 09:00:51.014: E/AndroidRuntime(9574): at android.text.TextUtils.split(TextUtils.java:332)
07-03 09:00:51.014: E/AndroidRuntime(9574): at com.example.rkscontacts_list.RKsContacts_Main$4.onClick(RKsContacts_Main.java:141)
07-03 09:00:51.014: E/AndroidRuntime(9574): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
07-03 09:00:51.014: E/AndroidRuntime(9574): at android.os.Handler.dispatchMessage(Handler.java:102)
07-03 09:00:51.014: E/AndroidRuntime(9574): at android.os.Looper.loop(Looper.java:136)
07-03 09:00:51.014: E/AndroidRuntime(9574): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-03 09:00:51.014: E/AndroidRuntime(9574): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 09:00:51.014: E/AndroidRuntime(9574): at java.lang.reflect.Method.invoke(Method.java:515)
07-03 09:00:51.014: E/AndroidRuntime(9574): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-03 09:00:51.014: E/AndroidRuntime(9574): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-03 09:00:51.014: E/AndroidRuntime(9574): at dalvik.system.NativeStart.main(Native Method)
推荐答案
请按照以下步骤操作:
提取存储在SharedPreferences
中的值
String value = prefs.getString(<Key>, <DefaultValue>);
附加到提取的值
String appendedValue = append(value, newValue);
将结果写回SharedPreferences
editor.putString(<Key>, appendedValue).commit();
这篇关于将更多值附加到共享首选项而不是覆盖现有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!