I have Player class which has a name and a arraylist of scores. Scores gets added dynamically with a button. I want to use shared preferences to save the Arraylist<Player> players which I created in my main activity. I have tried a couple of solutions with no success.
What I tried so far:
Links: Save ArrayList to SharedPreferences
When I used ObjectSerializer class I cant import package org.apache.pig.impl.util; and this causes to get errors such as :
because WrappedIOException is in that package.
Tinydb: Tiny db seemed liked a good solution but I get error when I retrive my data. These are the methods I changed from the java file:
public void putListObject(String key, ArrayList<Player> playerArray){
checkForNullKey(key);
Gson gson = new Gson();
ArrayList<String> playerStrings = new ArrayList<String>();
for(Player player : playerArray){
playerStrings.add(gson.toJson(player));
}
putListString(key, playerStrings);
}
public ArrayList<Player> getListObject(String key, Class<?> mClass) {
Gson gson = new Gson();
ArrayList<String> objStrings = getListString(key);
ArrayList<Player> objects = new ArrayList<Player>();
for (String jObjString : objStrings) {
Object value = gson.fromJson(jObjString, mClass);
objects.add((Player) value);
}
return objects;
}
public void putListObject(String key, ArrayList<Player> playerArray){
checkForNullKey(key);
Gson gson = new Gson();
ArrayList<String> playerStrings = new ArrayList<String>();
for(Player player : playerArray){
playerStrings.add(gson.toJson(player));
}
putListString(key, playerStrings);
}
Player Class:
public class Player implements Serializable {
private String name;
private ArrayList<Integer> scores = new ArrayList<>();
public Player(String name){
this.name = name;
}
public ArrayList<Integer> getScores() {
return scores;
}
public void setScore(int score) {
scores.add(score);
}
If anyone have a good solution on how to store and retrive my player arraylist please share

serializemethod get rid of the red lined code... and also you don't truly need to return anything so you can get rid of that too.