问题描述
我需要在共享首选项中保存一些字符串数组,然后再获取它们.我试过这个:
I need to save on shared preferences some array of Strings and after that to get them.I tried this :
prefsEditor.putString(PLAYLISTS, playlists.toString());
其中播放列表是一个 String[]
prefsEditor.putString(PLAYLISTS, playlists.toString());
where playlists is a String[]
并得到:
playlist= myPrefs.getString(PLAYLISTS, "playlists");
其中播放列表是一个 String
但它不起作用.
playlist= myPrefs.getString(PLAYLISTS, "playlists");
where playlist is a String
but it is not working.
我该怎么做?有人可以帮我吗?
How can I do this ? Can anyone help me?
提前致谢.
推荐答案
您可以像这样创建自己的数组字符串表示形式:
You can create your own String representation of the array like this:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < playlists.length; i++) {
sb.append(playlists[i]).append(",");
}
prefsEditor.putString(PLAYLISTS, sb.toString());
然后,当您从 SharedPreferences 获取字符串时,只需像这样解析它:
Then when you get the String from SharedPreferences simply parse it like this:
String[] playlists = playlist.split(",");
这应该可以完成工作.
这篇关于从共享首选项中放置和获取字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!