简体   繁体   中英

how to save a list of objects in Realm Android?

I want to save an object that has a list, the problem is that when I add the list it shows me an error

this is the object:

public class SaveRealm extends RealmObject {

    private String id;
    private RealmList<AnswersBean> answers;

    public SaveRealm(){}
    public SaveRealm(String id, RealmList<AnswersBean> answers) {
        this.id = id;
        this.answers = answers;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }


    public RealmList<AnswersBean> getAnswers() {
        return answers;
    }

    public void setAnswers(RealmList<AnswersBean> answers) {
        this.answers = answers;
    }
}

this is AnswersBean:

    public class AnswersBean extends RealmObject {
    private String text;
    private boolean belongsToCurrentUser;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public boolean isBelongsToCurrentUser() {
        return belongsToCurrentUser;
    }

    public void setBelongsToCurrentUser(boolean belongsToCurrentUser) {
        this.belongsToCurrentUser = belongsToCurrentUser;
    }
    public AnswersBean(){}
    public AnswersBean(String text, boolean belongsToCurrentUser) {
        this.text = text;
        this.belongsToCurrentUser = belongsToCurrentUser;
    }

}

and in the activity where I save the realm I do the following:

final AnswersBean receptor = new AnswersBean("test", false);
RealmList<AnswersBean> answersList = new RealmList<>();
answersList.add(receptor);
 SaveRealm saveRealm = myRealm.createObject(SaveRealm.class);
 saveRealm.setId("s");
 saveRealm.setAnswers(answersList);
 myRealm.commitTransaction();

but I get the following error

java.lang.NullPointerException: Attempt to invoke interface method 'long io.realm.internal.Row.getIndex()' on a null object reference

I get this error on the line saveRealm.setAnswers(answersList)

How can I save the list?

In your SaveRealm class, the answers field is a list. When you create an object using myRealm.createObject (with what I can only assume is a typo of ChatRealm.class ) that member is created. You cannot reassign that member to another list, rather you would modify the existing list.

For example, your SaveRealm.setAnwers method could look like this instead:-

public void setAnswers(RealmList<AnswersBean> answers) {
    this.answers.clear();
    this.answers.addAll(answers);
}

try this

public T addObject(Class<T> m_type, T obj) {
    T result = null;
    try {
        Realm realm = Realm.getDefaultInstance();
        realm.beginTransaction();
        result = realm.copyToRealmOrUpdate(obj);
        realm.commitTransaction();
        realm.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

here T is the generic object it will be your realm class

try this

try(Realm realm = Realm.getDefaultInstance()) {
                        realm.executeTransaction(new Realm.Transaction() {
                            @Override
                            public void execute(Realm realm) {
                                 RealmList<News> _newsList = new RealmList<>();
                                _newsList.addAll(myCustomArrayList);
                                realm.insertOrUpdate(_newsList); // <-- insert unmanaged to Realm

                            }
                        });
                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM