Android 从共享首选项检索Arraylist
我正在处理Android 从共享首选项检索Arraylist,android,arraylist,sharedpreferences,Android,Arraylist,Sharedpreferences,我正在处理共享首选项。基本上,我正在将ArrayList保存到shared preferences中,它们工作正常。现在我想从共享首选项中检索数组列表,但我得到的是空值。ArrayList正在从首选项中检索并显示其大小。但数据未设置为字符串。如何从共享首选项检索ArrayList。 这是我的密码 public void saveRootCategory(List<Category> categories){ preferences = mCtx.getSharedPrefer
共享首选项
。基本上,我正在将ArrayList
保存到shared preferences
中,它们工作正常。现在我想从共享首选项
中检索数组列表
,但我得到的是空值。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;
}
public void saveRootCategory(列出类别){
preferences=mCtx.getSharedReferences(共享首选项名称,模式专用项);
editor=preferences.edit();
editor.putInt(“RootCategory”,categories.size());
对于(int i=0;i您缺少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);
}
当您试图将数组列表
保存到共享首选项
中时
我建议您可以使用库
这与首选项
操作一样,可以非常快速地直接保存您的阵列列表
您还可以使用它直接存储基本数据类型和模型类
注意:它还减少了代码行。在setData()
->editor.putin(“CategoryId”,CategoryId);
上添加一些断点并调试代码是必要的
顺便说一句,在API 9或更高版本的设备上运行的调用editor.apply()
或editor.commit()
似乎没有什么区别。用以下方法替换这两种方法:
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());
}
public void saveRootCategory(列出类别){
preferences=mCtx.getSharedReferences(共享首选项名称,模式专用项);
editor=preferences.edit();
putString(“RootCategory”,new Gson().toJson(categories));
editor.apply();
}
公共数组列表getRootCategoy(){
preferences=mCtx.getSharedReferences(共享首选项名称,模式专用项);
String data=preferences.getString(“RootCategory”,”);
返回new Gson().fromJson(数据,new TypeToken(){}.getType());
}
没有第三方库需要使用Gson(内置)库,它可以轻松实现您想要实现的功能。我曾经遇到过同样的空问题
下面是我如何解决它的
我有一个名为language
language = new ArrayList<String>
为了保存这个数组列表,我创建了一个hashSet
Set<String> set = new HashSet<String>();
然后将其返回到language
array listonCreate
set.addAll(language);
.putStringSet("yourKey", set);
.commit();
prefs=this.getSharedPreferences("yourPrefsKey",Context.MODE_PRIVATE);
edit=prefs.edit();
set = prefs.getStringSet("yourKey", null);
language = new ArrayList<String>(set);
edit.remove("yourKey").commit();
prefs=this.getSharedReferences(“yourPrefsKey”,Context.MODE\u PRIVATE);
edit=prefs.edit();
set=prefs.getStringSet(“yourKey”,null);
语言=新数组列表(集合);
edit.remove(“yourKey”).commit();
请记住每次都要删除,否则它将再次创建null公共void setData(最终类别)
该方法使用相同的键(“CategoryId”、“CategoryName”)保存列表中的每个项目。
但是,您可以使用“CategoryId”+index
从SharedReference获取值。
很明显,你永远不会用这种方式得到正确的答案
@穆拉特的回答纠正了这个方法,它可以很好地工作。
但我认为这不是保存列表的好方法。如果您确实想使用SP保存列表,我建议您先使用Gson
或任何其他方法将对象转换为String
。这样会更好。什么是索引请解释it@ansarabbas您可以通过调用getString(“CategoryName”+i)来获取名称
但是你保存它时没有i
。索引就是你的i
。但是我如何用索引保存列表呢?@ansarabbas你试过我的答案吗?
prefs=this.getSharedPreferences("yourPrefsKey",Context.MODE_PRIVATE);
edit=prefs.edit();
set = prefs.getStringSet("yourKey", null);
language = new ArrayList<String>(set);
edit.remove("yourKey").commit();