Skip to content Skip to sidebar Skip to footer

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, i

Solution 1:

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

Solution 2:

Unfortunately you can't do what you want. I presume the method you're searching for is startsWith() and not startAt() (to match all words begining with a substring). This method hasn't been implemented yet in Firebase, too bad.

You have to use an external service like ElasticSearch but that would mean you must have a server which is probably one of reason you actually use firebase... Anyway this is a link for implementing ElasticSearch:

https://firebase.googleblog.com/2014/01/queries-part-2-advanced-searches-with.html

Otherwise you'll have to figure out how to get rid of your search bar...

Good luck!

Solution 3:

Worked for me:

Queryquery= MyApplication.sReferenceOrders.orderByChild(Constants.STATUS).startAt("1").endAt("1\\uf8ff");

Solution 4:

I think you are trying to implement search feature to your app .. this is my query

privatevoidsearchForMatch(String keyword) {
    Log.d(TAG, "SearchForMatch: searching for match");
    mUserList.clear();
    if (keyword == null) {
        mUserList.clear();
    } else {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
        Query query = reference.child("where you want to search in database ")
                .orderByChild("what do you want search from your database").startAt(keyword).endAt(keyword + "\uf8ff");
        query.addListenerForSingleValueEvent(newValueEventListener() {
            @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot singlesnapshot : dataSnapshot.getChildren()) {
                    Log.d(TAG, "onDataChange: found user" + singlesnapshot.getValue(User.class).toString());
                    mUserList.add(singlesnapshot.getValue(User.class));
                   this is my list adapter for displaying search results // updateUsersList();


                }
            }

            @OverridepublicvoidonCancelled(DatabaseError databaseError) {

            }
        });
    }


}

If you don't know how it works then check out this tutorial on youtube.

Solution 5:

I too was looking for this solution, thanks. I tried it, and even works with this:

startAt('ab').endAt('ab\w|\W');

Post a Comment for "Android - Firebase Query Startat Not Working As Expected"