Android 在 SharedPreferences 中存储字符串数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10813841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:05:04  来源:igfitidea点击:

Storing a String array in the SharedPreferences

androidstoragesharedpreferences

提问by noloman

I was wondering if it could be possible to save in the shared preferences an array of Strings, in a way that, every time we save a certain String, we store it in that array.

我想知道是否可以在共享首选项中保存一个字符串数组,这样,每次我们保存某个字符串时,我们都会将它存储在该数组中。

For example I have a list of locations with a certain ID that I want to mark as favorite. The ideal situation would be, having an array and saving a certain location ID (let's call it Location1) in that array, so next time I want to mark a new location as favorite (let's call it Location2), I retrieve that array (which so far contains Location1) and add the ID of this new location I want to add (Location2).

例如,我有一个带有特定 ID 的位置列表,我想将其标记为收藏。理想的情况是,有一个数组并在该数组中保存某个位置 ID(我们称之为 Location1),所以下次我想将一个新位置标记为最喜欢的(我们称之为 Location2)时,我检索该数组(我们称之为到目前为止包含 Location1) 并添加我想添加的这个新位置的 ID (Location2)。

Android has methods to store primitive objects, but not for arrays. Any idea in order to do this, please?

Android 有存储原始对象的方法,但没有存储数组的方法。有什么想法可以做到这一点吗?

采纳答案by David Wasser

Write methods to read and write a serialized array. This shouldn't be too difficult. Just flatten the array of strings into a single string that you store in the preferences. Another option would be to convert the array into an XML structure that you then store in the preferences, but that is probably overkill.

用于读取和写入序列化数组的写入方法。这应该不会太难。只需将字符串数组展平为您存储在首选项中的单个字符串。另一种选择是将数组转换为 XML 结构,然后将其存储在首选项中,但这可能有点过头了。

回答by Sherif elKhatib

This is doable: I was just blogging about it:

这是可行的:我只是在写博客

SAVE YOUR ARRAY

保存您的阵列

//String array[]
//SharedPreferences prefs
Editor edit = prefs.edit();
edit.putInt("array_size", array.length);
for(int i=0;i<array.length; i++)
    edit.putString("array_" + i, array[i]);
edit.commit();

RETRIEVE YOUR ARRAY

检索您的数组

int size = prefs.getInt("array_size", 0);
array = new String[size];
for(int i=0; i<size; i++)
    prefs.getString("array_" + i, null);


Just wrote that so there might be typos.

刚刚写的,可能有错别字。

回答by beenjaminnn

You could make the array a JSON array and then store it like this:

您可以将数组设为 JSON 数组,然后像这样存储它:

SharedPreferences settings = getSharedPreferences("SETTINGS KEY", 0);
SharedPreferences.Editor editor = settings.edit();

JSONArray jArray = new JSONArray();
try {
    jArray.put(id);
} catch (JSONException e) {
    e.printStackTrace();
}

editor.putString("jArray", jArray.toString());
editor.commit();

You can then get the array like this:

然后,您可以像这样获取数组:

SharedPreferences settings = getSharedPreferences("SETTINGS KEY", 0);
try {
    JSONArray jArray = new JSONArray(settings.getString("jArray", ""));
} catch (JSONException e) {
    e.printStackTrace();
}

Just an alternative solution that I have used in the past

只是我过去使用的替代解决方案