I am attempting to create a class that will contain methods for saving and loading an ArrayList of objects into SharedPreferences. However, when I attempt to load back data I had previously saved. It is as if it has not saved. I am using Googles Gson Libraries to help.
Instance_Read_Write.java
public class Instance_Read_Write {
public void saveData(Context context, ArrayList<Example> Examples){
SharedPreferences sharedPreferences = context.getSharedPreferences("instanceKey",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(Examples);
editor.putString("list", json);
editor.apply();
}
public void loadData(Context context, ArrayList<Example> Examples) {
SharedPreferences sharedPreferences = context.getSharedPreferences("instanceKey",Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("list", null);
Type type = new TypeToken<ArrayList<Example>>() {}.getType();
Examples = gson.fromJson(json, type);
if (Examples == null) {
Examples = new ArrayList<>();
}
}
}
and this is how I am referencing them.
MainActivity.java
Instance_Read_Write instance_read_write = new Instance_Read_Write();
instance_read_write.loadData(context,Examples);
Examples.add(new Example("test"));
...
...
...
instance_read_write.saveData(context,Examples);
Is there a way to make this work? or perhaps a better way to do this?
This addresses one error in your posted code namely the treatment of the Examples
parameter in the method loadData
method.
Since it seems your intention of the loadData
is to always produce an ArrayList then I'd recommend returning the instance. Alternatively, you would have to first create an empty ArrayList and pass that in and then operate on it.
So using the approach of returning the ArrayList:
public ArrayList<Example> loadData(Context context) {
ArrayList<Example> result;
SharedPreferences sharedPreferences = context.getSharedPreferences("instanceKey",Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("list", null);
Type type = new TypeToken<ArrayList<Example>>() {}.getType();
result = gson.fromJson(json, type);
if (result == null) {
result = new ArrayList<>();
}
return result;
}
// ... and the calling code (in MainActivity) looks like:
Examples = instance_read_write.loadData(context);
Not to stoke up a pass-by-value vs pass-by-reference discussion - it appears you are expecting Example
actual parameter to be updated by assigning it in the body of the method - which it will not - you can only operate on the object to affect it: https://stackoverflow.com/a/40523/2711811.
To complete the topic, here's a solution which would continue your intention of passing in the Examples
object:
// In calling method...
Examples = new ArrayList<Example>;
instance_read_write.loadData(context,Examples);
//...in class Instance_Read_Write...
public void loadData(Context context, ArrayList<Example> Examples) {
SharedPreferences sharedPreferences = context.getSharedPreferences("instanceKey",Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("list", null);
Type type = new TypeToken<ArrayList<Example>>() {}.getType();
ArrayList<Example> t = gson.fromJson(json, type);
if (t != null) {
Examples.addAll(t);
}
}