9

I'm trying to save an object list to the Shared Preferences using TinyDB's putListObject function, but I'm getting a Wrong 2nd argument type error. I can use the putObject function just fine, the error only appears when I use the putListObject function.


Player Class:

public class Player {

    private String name = "";
    private int score = 0;

    public Player(String name, int score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

putListObject function:

 public void putListObject(String key, ArrayList<Object> objArray){
        checkForNullKey(key);
        Gson gson = new Gson();
        ArrayList<String> objStrings = new ArrayList<String>();
        for(Object obj : objArray){
            objStrings.add(gson.toJson(obj));
        }
        putListString(key, objStrings);
    }

How I used the function:

ArrayList<Player> playerList = new ArrayList<Player>();

TinyDB tinydb = new TinyDB(this);
tinydb.putListObject("players", playerList);

The error I got:

putListObject (String java.util.ArrayList<java.lang.Object>) in TinyDB cannot be applied to (String java.util.ArrayList<com.example.package.Player>)


Help would be much appreciated!

Lin
  • 87
  • 1
  • 1
  • 6

2 Answers2

13

kcochibili Developer of TinyDB answered this on github. He says, you must cast your custom objects to object before trigger putListObject.

A sample for putting Custom Object ArrayList:

ArrayList<Player> players = new ArrayList<Player>();
ArrayList<Object> playerObjects = new ArrayList<Object>();

for(Player a : players){
    playerObjects.add((Object)a);
}

TinyDB tinydb = new TinyDB(this);
tinydb.putListObject("players", playerObjects);

Also when you want to get values from DB it will give ArrayList< Object >. So you may want to cast them back to your custom object.

A sample for this:

TinyDB tinydb = new TinyDB(this);
ArrayList<Object> playerObjects = tinydb.getListObject("players", Player.class);
ArrayList<Player> players = new ArrayList<Player>();

for(Object objs : playerObjects){
    players.add((Player)objs);
}

You can use all custom objects by casting. The other way i prefer is, adding get and put methods for all custom objects to TinyDB Class. For example :

public void putListPlayer(String key, ArrayList<Player> playerList){
    checkForNullKey(key);
    Gson gson = new Gson();
    ArrayList<String> objStrings = new ArrayList<String>();
    for(Player player: playerList){
        objStrings.add(gson.toJson(player));
    }
    putListString(key, objStrings);
}

//No need Class<?> mClass parameter. Because we know it is Player!
public ArrayList<Player> getListPlayer(String key){
    Gson gson = new Gson(); 

    ArrayList<String> objStrings = getListString(key);
    ArrayList<Player> playerList =  new ArrayList<Player>();

    for(String jObjString : objStrings){
        Player player  = gson.fromJson(jObjString,  Player.class);
        playerList.add(player);
    }
    return playerList;
}
Umair
  • 6,366
  • 15
  • 42
  • 50
Fatih K.
  • 393
  • 3
  • 6
  • 2
    Faith K, I know "thank yous" are passé and frowned upon, but you saved me hours of work and I wanted to express more appreciation than just a simple up-vote! A full working example would, however, go a long way in explaining it for others. – Trasd Jul 29 '19 at 21:44
  • What will happen if I call `putListPlayer` using the same key multiple times? Will it keep updating the value at that key or will it throw an error? – Ravish Jha Sep 02 '20 at 19:49
  • Hii @Faith K Thanks. I have used your second way for my custom array list storage. – Dharmishtha Dec 16 '20 at 06:13
  • @RavishJha calling ``putListPlayer`` again. or any TinyDB function will update the value that was last saved with the specified key. – kc ochibili Feb 09 '21 at 07:13
7

Although any instance of Player is an Object, an ArrayList of Players is not the same as an ArrayList of Objects. Change your method signature to :

putListObject(String key, ArrayList<Player> objArray)

and for loop to:

for(Player player : objArray){
   objStrings.add(gson.toJson(player));
}
Würgspaß
  • 4,660
  • 2
  • 25
  • 41