PUT和GET从共享preferences String数组 [英] Put and get String array from shared preferences

查看:102
本文介绍了PUT和GET从共享preferences String数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要保存在共享preferences字符串的一些阵列,之后让他们。 我想这样的:

I need to save on shared preferences some array of Strings and after that to get them. I tried this :

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

和获得:

播放列表=我的prefs.getString(播放列表,播放列表); 其中,播放列表是一个字符串但它不能正常工作。

playlist= myPrefs.getString(PLAYLISTS, "playlists"); where playlist is a String but it is not working.

我怎样才能做到这一点?谁能帮我?

How can I do this ? Can anyone help me?

在此先感谢。

推荐答案

您可以创建自己的字符串再这样数组的presentation:

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());

然后,当你从共享preferences字符串简单地分析它是这样的:

Then when you get the String from SharedPreferences simply parse it like this:

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

这应该做的工作。

这篇关于PUT和GET从共享preferences String数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆