简体   繁体   中英

Android - Realm how to query by boolean

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 .

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 ;-)

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"

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