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
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.
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.
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.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With