【问题标题】:Retrieve Arraylist from Shared Preferences从共享首选项中检索 Arraylist
【发布时间】:2019-03-02 04:21:15
【问题描述】:

我正在处理shared Preferences。基本上,我将ArrayList 保存到shared preferences,它们工作正常。现在我想从Shared preferences 中检索ArrayList,但我得到了空值。 ArrayList 正在从首选项中检索并显示它的大小。但数据未设置为字符串。我如何从共享首选项中检索 ArrayList。 这是我的代码

public void saveRootCategory(List<Category> categories){
    preferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, MODE_PRIVATE);
    editor = preferences.edit();
    editor.putInt("RootCategory",categories.size());
    for (int i=0;i<categories.size();i++){
        setData(categories.get(i));
    }
    editor.apply();
}
public void setData(final Category category){
    categoryId = category.getId();
    categoryName = category.getCategoryName();
    editor.putInt("CategoryId",categoryId);
    editor.putString("CategoryName",categoryName);
}
public  ArrayList<String> getRootCategoy() {
    preferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, MODE_PRIVATE);
    ArrayList<String> rootCategories = new ArrayList<>();
    rootCategories.clear();
    int size = preferences.getInt("RootCategory", 0);
    for(int i=0;i<size;i++) {
        int Id = preferences.getInt("CategoryId" + i,0);
        String name = preferences.getString("CategoryName" + i ,"");
        rootCategories.add(String.valueOf(Id));
        rootCategories.add(name);
    }
    return rootCategories;
}

【问题讨论】:

    标签: android arraylist sharedpreferences


    【解决方案1】:

    我曾经遇到过同样的空问题
    这是我解决它的方法
    我有一个名为language 的arrayList

    language = new ArrayList<String>  
    

    我必须在按钮单击时添加用户在编辑文本上写的所有内容,而不是重复任何冗余值

        if(!language.contains(value)) {
             language.add(value);
    }  
    

    为了保存这个数组列表,我创建了一个hashSet

     Set<String> set = new HashSet<String>();  
    

    并保存所有onPause

    set.addAll(language);
    .putStringSet("yourKey", set);
    .commit();  
    

    并将其检索回language数组列表onCreate

    prefs=this.getSharedPreferences("yourPrefsKey",Context.MODE_PRIVATE);
            edit=prefs.edit();
    set = prefs.getStringSet("yourKey", null);
                language = new ArrayList<String>(set);
                edit.remove("yourKey").commit();  
    

    记得每次都删除,否则会再次创建null

    【讨论】:

      【解决方案2】:

      您缺少setData 中的索引。改成

      public void setData(final Category category, int index){
          categoryId = category.getId();
          categoryName = category.getCategoryName();
          editor.putInt("CategoryId" + index, categoryId);
          editor.putString("CategoryName" + index, categoryName);
      }
      

      【讨论】:

      • 什么是索引请解释一下
      • @ansarabbas 您尝试通过调用getString("CategoryName" + i) 来获取名称,但您在没有i 的情况下保存了它。索引是你的i
      • 但是我如何用索引保存列表?
      • @ansarabbas 你有没有试过我的回答?
      【解决方案3】:

      当您尝试将ArrayList 保存到share preference 中时。

      我建议你可以使用 PaperDB Library

      这非常快,直接保存你的ArrayListpreference一样。

      您也可以使用它直接存储 Primitive 数据类型和模型类。

      注意:它也减少了代码行数。

      【讨论】:

        【解决方案4】:

        setData() 上添加一些断点 -> editor.putInt("CategoryId",categoryId); 并调试您的代码是必要的。

        顺便说一句,在 API 9 或更高版本的设备上运行 editor.apply()editor.commit() 似乎没有区别。

        【讨论】:

          【解决方案5】:

          用这些替换你的两种方法:

          public void saveRootCategory(List<Category> categories){
              preferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, MODE_PRIVATE);
              editor = preferences.edit();
              editor.putString("RootCategory",new Gson().toJson(categories));
              editor.apply();
          }
          
          public  ArrayList<Category> getRootCategoy() {
              preferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, MODE_PRIVATE);
              String data = preferences.getString("RootCategory","");
              return new Gson().fromJson(data, new TypeToken<List<Category>>(){}.getType());
          }
          

          不需要第三方库,使用 Gson(inbuilt) 库可以轻松完成您想要实现的目标。

          【讨论】:

            【解决方案6】:

            public void setData(final Category category)
            该方法使用相同的键(“CategoryId”,“CategoryName”)来保存列表中的每个项目。 但是,您使用 "CategoryId" + index 从 SharedPreference 获取值。
            显然,这样你永远无法得到正确的答案……

            @Murat 的回答更正了方法,它可以很好地工作。
            但我认为这不是保存列表的好方法。如果您确实想使用 SP 来保存列表,我建议先使用Gson 或任何其他方式将您的对象转换为String。效果会更好。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-09-10
              相关资源
              最近更新 更多