android开发分享从共享首选项中获取并获取string数组

我需要保存共享首选项的一些string数组,然后才能得到它们。 我试过这个:

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

并得到:

playlist= myPrefs.getString(PLAYLISTS, "playlists"); 播放列表是一个String但它不工作。

我该怎么做? 任何人都可以帮我吗?

提前致谢。

    你可以像这样创build你自己的数组的string表示forms:

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

    然后当你从SharedPreferences获取String时,简单地parsing它就像这样:

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

    这应该做的工作。

    从API级别11,您可以使用putStringSet和getStringSet来存储/检索string集:

     SharedPreferences pref = context.getSharedPreferences(TAG, Context.MODE_PRIVATE); SharedPreferences.Editor editor = pref.edit(); editor.putStringSet(SOME_KEY, someStringSet); editor.commit(); SharedPreferences pref = context.getSharedPreferences(TAG, Context.MODE_PRIVATE); Set<String> someStringSet = pref.getStringSet(SOME_KEY); 

    您可以使用JSON将您的数组序列化为string并将其存储在首选项中。 看到我的答案和示例代码在这里有一个类似的问题:

    如何编写代码,以使android的数组共享首选项?

      以上就是android开发分享从共享首选项中获取并获取string数组相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/518086.html

      (0)
      上一篇 2020年12月5日
      下一篇 2020年12月5日

      精彩推荐