2

I have started to use ´Realm´ and I cannot figure out how to get all the results with the value equals to true.

Take a look my code below:

Java Class

@RealmClass
public class Bookmark extends RealmObject {
    private java.lang.String IdBookMark;
    private boolean BookMarkActive;

    public String getIdBookMark() {
        return IdBookMark;
    }

    public void setIdBookMark(String idBookMark) {
        IdBookMark = idBookMark;
    }

    public boolean isBookMarkActive() {
        return BookMarkActive;
    }

    public void setBookMarkActive(boolean bookMarkActive) {
        BookMarkActive = bookMarkActive;
    }
}

Function:

private boolean AnyBookMark (){
    Realm realm = Realm.getInstance(getContext());
    RealmQuery<Bookmark> query = realm.where(Bookmark.class);
    query.contains("BookMarkActive","true");
    RealmResults<Bookmark> userBookmarks = query.findAll();
    return userBookmarks.isEmpty();
}

Error:

FATAL EXCEPTION: main Process: gon250.dublinbikes, PID: 2624 java.lang.IllegalArgumentException: Field 'BookMarkActive': type mismatch. Was STRING, expected BOOLEAN. at io.realm.RealmQuery.getColumnIndices(RealmQuery.java:146) at io.realm.RealmQuery.contains(RealmQuery.java:816) at io.realm.RealmQuery.contains(RealmQuery.java:802) at tabs.Tab2.AnyBookMark(Tab2.java:55) at tabs.Tab2.onCreateView(Tab2.java:33)

Version:

compile 'io.realm:realm-android:0.78.0'

What is the proper way to get all the results with BookMarkActive equals to true.

gon250
  • 3,405
  • 6
  • 44
  • 75

2 Answers2

9

Instead of query.contains("BookMarkActive","true"); you can do query.equalTo("BookMarkActive", true);.

Btw, 0.78.0 is a really old version. I recommend that you upgrade soon ;-)

geisshirt
  • 2,457
  • 1
  • 17
  • 20
  • I can't do it.. I get an error because the bool. I have changed my object class so `BookMarkActive` is now a string and is working. Maybe is because the version isnt it? in case I update the Realm, Have I to change my code? – gon250 Jan 14 '16 at 17:25
  • Updating to for example 0.87 will require changes to your code as we have changed the API and will do so until 1.0. – geisshirt Jan 14 '16 at 17:31
1

Function should be something like this:

private boolean anyBookMark() {
    Realm realm = Realm.getInstance(getContext());
    RealmQuery<Bookmark> query = realm.where(Bookmark.class);
    query.equalTo("BookMarkActive", "true");
    RealmResults<Bookmark> userBookmarks = query.findAll();
    return userBookmarks.isEmpty();
}

BookMarkActive column should be String type. If it is some other type then corresponding value should be passed rather that "true"

Ziem
  • 6,579
  • 8
  • 53
  • 86
Simpalm
  • 1
  • 5