Reputation: 11
I am working on shared Preferences
. Basically, I am saving ArrayList
to shared preferences
which are working fine. Now I want to retrieve the ArrayList
from Shared preferences
but I am getting null. ArrayList is retrieving from preferences and also showing it's size. but data is not being set to string. How I retrieve the ArrayList from shared Preferences.
here is my code
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;
}
Upvotes: 0
Views: 184
Reputation: 1
public void setData(final Category category)
The method use the same key("CategoryId", "CategoryName") to save every item in the list.
However, you use "CategoryId" + index
to get the value from SharedPreference.
Obviously, you can never get the right answer in this way...
@Murat's answer correct the method, and it can work well.
But I don't think it's a good way to save a List. If you do want to use SP to save Lists, I suggest Gson
or any other way to translate your object into String
first. It will work better.
Upvotes: 0
Reputation: 8305
Replace your both methods with these:
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());
}
No third party library needed use Gson(inbuilt) library which can easily do what you want to achieve.
Upvotes: 0
Reputation: 842
I used to have the same null problem
Here's how I solved it
I have an arrayList named language
language = new ArrayList<String>
I had to add everything the user writes on the edittext on button click not repeating any redundant value
if(!language.contains(value)) {
language.add(value);
}
To save this arraylist I created a hashSet
Set<String> set = new HashSet<String>();
and save all of them onPause
set.addAll(language);
.putStringSet("yourKey", set);
.commit();
and retrive it back to language
array list 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();
remember to remove everytime or it will again create null
Upvotes: 1
Reputation: 370
Add some breakpoints on setData()
-> editor.putInt("CategoryId",categoryId);
and debug your code are necessary.
BTW, it seems that there's no difference you call either editor.apply()
or editor.commit()
running on API 9 or above devices.
Upvotes: 0
Reputation: 3265
As you are trying to save the ArrayList
into the share preference
.
I Suggest you can use PaperDB Library.
Which is very fast and directly save your ArrayList
same as preference
doing.
You can also use it to store the Primitive datatypes and model class directly.
Note: It also reduces the code lines.
Upvotes: 0
Reputation: 37594
You are missing the index in setData
. Change it to
public void setData(final Category category, int index){
categoryId = category.getId();
categoryName = category.getCategoryName();
editor.putInt("CategoryId" + index, categoryId);
editor.putString("CategoryName" + index, categoryName);
}
Upvotes: 0