【问题标题】:Put and get String array from shared preferences从共享首选项中放置和获取字符串数组
【发布时间】:2018-02-06 22:41:18
【问题描述】:

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

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

得到:

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

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

提前致谢。

【问题讨论】:

    标签: android arrays string sharedpreferences


    【解决方案1】:

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

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

    这应该可以完成工作。

    【讨论】:

    • 那你怎么做getString()?你用什么作为第二个参数?
    • @IgorG.,这完全取决于您,实现您自己的逻辑来检查该字符串中的项目数。
    • 这个问题是字符串不能包含用于将它们彼此分开的字符。使用此方法前需要编码。
    • @Mario Lenci,我使用 ";;delimiter;;"作为单独的字符串
    • 那么你的解决办法是什么?
    【解决方案2】:

    从 API 级别 11 开始,您可以使用 putStringSet 和 getStringSet 来存储/检索字符串集:

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

    【讨论】:

    • 我认为如果有人从 API 级别 11 或更高级别构建应用程序,这比接受的答案更容易。
    • 问题是Set没有排序。与 String[] 相比
    • 我有一个字符串数组,其值为 a,0b,1 现在点击设置,我想更改这些值。如何在 this 中使用 putStringSet。
    • @pa1pal,你可能应该创建一个新问题,并在那里准确地解释你想做什么以及到目前为止你做了什么
    【解决方案3】:

    您可以使用 JSON 将数组序列化为字符串并将其存储在首选项中。在此处查看我的答案和示例代码以了解类似问题:

    How can write code to make sharedpreferences for array in android?

    【讨论】:

      【解决方案4】:
      HashSet<String> mSet = new HashSet<>();
                      mSet.add("data1");
                      mSet.add("data2");
      saveStringSet(context, mSet);
      

      在哪里

      public static void saveStringSet(Context context, HashSet<String> mSet) {
          SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
          SharedPreferences.Editor editor = sp.edit();
          editor.putStringSet(PREF_STRING_SET_KEY, mSet);
          editor.apply();
      }
      

      public static Set<String> getSavedStringSets(Context context) {
          SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
          return sp.getStringSet(PREF_STRING_SET_KEY, null);
      }
      
      private static final String PREF_STRING_SET_KEY = "string_set_key";
      

      【讨论】:

      • 正如@Tomáš Hubálek 所述,Set 没有排序,因此可能会改变程序的行为。
      【解决方案5】:

      如果您想了解更多信息Click here

      ,请使用此简单功能将数组列表优先存储
       public static void storeSerializeArraylist(SharedPreferences sharedPreferences, String key, ArrayList tempAppArraylist){
          SharedPreferences.Editor editor = sharedPreferences.edit();
          try {
              editor.putString(key, ObjectSerializer.serialize(tempAppArraylist));
              editor.apply();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      

      以及如何从prefrence获取存储的数组列表

      public static ArrayList getSerializeArraylist(SharedPreferences sharedPreferences, String key){
          ArrayList tempArrayList = new ArrayList();
          try {
              tempArrayList = (ArrayList) ObjectSerializer.deserialize(sharedPreferences.getString(key, ObjectSerializer.serialize(new ArrayList())));
          } catch (IOException e) {
              e.printStackTrace();
          }
          return tempArrayList;
      }
      

      【讨论】:

      • 此链接已损坏。 ObjectSerializer 不是 Android 中的类。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      • 2011-11-25
      • 1970-01-01
      相关资源
      最近更新 更多