RealmList of String Type in Android

user2566484 picture user2566484 · Feb 16, 2016 · Viewed 20.6k times · Source

I'm using Realm for Local storage in Android. I'm getting following response form server.

[{
    "ListId": 10,
    "Names": ["Name1", "Name2", "Name3", "Name4"]
}]

Here is my Model

   public class Model extends RealmObject {

    private int ListId;
    private RealmList<String> Names = new RealmList<String>()

    public int getListId() {
        return ListId;
    }

    public void setListId(int listId) {
        ListId = listId;
    }

    public RealmList<String> getNames() {
        return Names;
    }

    public void setNames(RealmList<String> names) {
        Names = names;
    }

}

And I'm getting this for ArrayList

Type parameter 'java.lang.String' is not within its bound; should extend 'io.realm.RealmObject'.

Thanks.

Answer

Christian Melchior picture Christian Melchior · Feb 16, 2016

RealmLists doesn't support simple strings yet. so you have to wrap each String into its own object:

You can see a work-around here: Gson deserialization of List<String> into realmList<RealmString>

or here: https://realm.io/docs/java/latest/#primitive-lists