Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search for a value in firebase Android

I am saving data as follows in the Firebase:

structure

I want to find all records that have #Yahoo in their title. What will be the exact query for that?

I am confused with the random key created. I am not sure how to handle this so i am posting it here.

Firebase firebase = new Firebase(Constants.FIREBASE_URL_USER_TASKS).child(Utils.encodeEmail(unProcessedEmail));
        Query queryRef = firebase.orderByKey().startAt("#" + mHashTag).endAt("#" + mHashTag + "\uf8ff");

        queryRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                mTasksList.clear();
                mAdapter.notifyDataSetChanged();
                for (DataSnapshot task : dataSnapshot.getChildren()) {
                    mTasksList.add(task.getValue(TaskModel.class));
                }
                mAdapter.notifyItemRangeInserted(0, mTasksList.size());
                mSwipeToRefresh.post(new Runnable() {
                    @Override
                    public void run() {
                        mSwipeToRefresh.setRefreshing(false);
                    }
                });
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                mSwipeToRefresh.post(new Runnable() {
                    @Override
                    public void run() {
                        mSwipeToRefresh.setRefreshing(false);
                    }
                });
            }
        });
like image 826
Shajeel Afzal Avatar asked Dec 30 '15 21:12

Shajeel Afzal


People also ask

How do I get particular data from Firebase?

Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.

How do you filter data in Firebase Realtime Database?

We can filter data in one of three ways: by child key, by key, or by value. A query starts with one of these parameters, and then must be combined with one or more of the following parameters: startAt , endAt , limitToFirst , limitToLast , or equalTo .

Can retrieve data from Firebase Android?

After creating a new project, navigate to the Tools option on the top bar. Inside that click on Firebase. After clicking on Firebase, you can get to see the right column mentioned below in the screenshot. Inside that column Navigate to Firebase Realtime Database.


2 Answers

You cannot search for any item whose title contains #Yahoo. See: How to perform sql "LIKE" operation on firebase?

You can however search items whose title begins with #Yahoo:

Firebase firebase = new Firebase(Constants.FIREBASE_URL_USER_TASKS).child(Utils.encodeEmail(unProcessedEmail));

Query queryRef = firebase.orderByChild("title").startAt("#" + mHashTag)

To make this work well, you have to add an index to your Firebase rules:

"rules": {
  "userTasks": {
    "$email": {
      ".indexOn": "title" // index tasks in their title property
    }
  }
}
like image 160
Frank van Puffelen Avatar answered Sep 17 '22 14:09

Frank van Puffelen


This is working with the Google Firebase.

DatabaseReference mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
    Query query = mFirebaseDatabaseReference.child("userTasks").orderByChild("title").equalTo("#Yahoo");
    query.addValueEventListener(valueEventListener);

ValueEventListener valueEventListener = new ValueEventListener()
{
    @Override
    public void onDataChange(DataSnapshot dataSnapshot)
    {
        for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
        {
            //TODO get the data here

        }

    }

    @Override
    public void onCancelled(DatabaseError databaseError)
    {

    }
};
like image 41
Son Nguyen Thanh Avatar answered Sep 20 '22 14:09

Son Nguyen Thanh