Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Shared Preference TinyDB putListObject frunction

Tags:

java

android

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!

like image 739
user4932354 Avatar asked Jan 30 '16 11:01

user4932354


People also ask

How do I save an ArrayList of custom objects to SharedPreferences?

SharedPreferences appSharedPrefs = PreferenceManager . getDefaultSharedPreferences(this. getApplicationContext()); Gson gson = new Gson(); String json = appSharedPrefs. getString("MyObject", ""); Student mStudentObject = gson.

How can I get shared preference data?

Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

Can we save object in shared preferences?

We can store fields of any Object to shared preference by serializing the object to String. Here I have used GSON for storing any object to shared preference. Note : Remember to add compile 'com.


2 Answers

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;
}
like image 107
Fatih K. Avatar answered Oct 04 '22 03:10

Fatih K.


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));
}
like image 20
Würgspaß Avatar answered Oct 04 '22 02:10

Würgspaß