我需要在共享首选项中保存一些字符串数组,然后再获取它们。 我试过这个:
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/