android - 从共享首选项中放置和获取字符串数组

标签 android arrays string sharedpreferences

我需要在共享首选项中保存一些字符串数组,然后再获取它们。 我试过这个:

prefsEditor.putString(PLAYLISTS, playlists.toString()); 其中播放列表是 String[]

得到:

playlist= myPrefs.getString(PLAYLISTS, "playlists"); 其中播放列表是 String 但它不起作用。

我该怎么做?谁能帮帮我?

提前致谢。

最佳答案

您可以像这样创建自己的数组字符串表示形式:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < playlists.length; i++) {
    sb.append(playlists[i]).append(",");
}
prefsEditor.putString(PLAYLISTS, sb.toString());

然后,当您从 SharedPreferences 获取字符串时,只需像这样解析它:

String[] playlists = playlist.split(",");

这应该可以完成工作。

关于android - 从共享首选项中放置和获取字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7965290/

相关文章:

c - 数组搜索只需创建一个指针即可

android - 在已 root 的 Android 设备上,如何打开 < 1024 的端口?

c++ - 将数组传递给发送UDP数据包的函数

android - 如何使用 Volley 下载图像?

java - 使用 array[key]++ 递增数组值

c - 如何使 strtok 在标记末尾包含换行符?

python - Pandas - 将前导 "0"添加到字符串值,以便所有值都等于 len

c++ - 如何在构建时将文件内容获取到我的 C++ 字符串中?

android - Android 浏览器中奇怪的水平滚动

android - 使用 Android 读取 UTF-8 格式字符串的 XML 文件