Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Firebase query startAt not working as expected

I am developing an Android app using Firebase as backend. I am new to Firebase and stuck at a problem, well the problem is when I try to search for a record using startAt query, it returns results which does not starts with the keyword I enters.

Here's the data set

itemname
    -KK8vI8A5BZZp3Xo3FpA
       name: "abc"
    -KK8w3uoJdJ0hBSrq0CS
       name: "test"
    -KKAC1o9Vazyg9JLtDoQ
       name: "dude"

And here's the code snippit

Query query = firebase.child(Constants.KEY_ITEM_NAME).orderByChild("name").startAt("abc");
        query.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Iterator<DataSnapshot> i = dataSnapshot.getChildren().iterator();
                while (i.hasNext()) {
                    DataSnapshot d = i.next();
                    LOG(d.getKey(), d.getValue().toString());
                }
            }

So when I search for abc the response also includes test. Maybe I am doing something wrong or I am going the wrong way. Could anyone please point me in right direction.

P.S I am trying to use an AutocompleteTextView to search items.

Thanks

like image 435
Max Avatar asked Jun 13 '16 20:06

Max


People also ask

What is a firebase query reference?

A Firebase reference represents a particular location in your Database and can be used for reading or writing data to that Database location. The Query class (and its subclass, DatabaseReference) are used for reading data. Listeners are attached, and they will be triggered when the corresponding data changes.

Is class extending firebaseinstanceidservice ever called?

NEVER called. class extending FirebaseInstanceIdService, is NEVER called. Apparently this was required when I tried to use the method "getToken ()" through Firebase. A have also tried with and without these methods.

Do startat() and startafter() work with 0 index?

But they don't work at all. The result of a query does not depend on the values passed to startAt () / startAfter (), the cursor is always set on the 0 index. const startAt = 10; // Choose any value.


1 Answers

When you call orderByChild("name").startAt("abc") the database orders all items by their name property, skips the ones before abc and then returns them all.

If you're only looking to return children that match abc, you'd use equalTo():

query = firebase.child(Constants.KEY_ITEM_NAME).orderByChild("name").equalTo("abc");
like image 176
Frank van Puffelen Avatar answered Oct 11 '22 10:10

Frank van Puffelen