Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve specific node from firebase database in android

I am trying to use Firebase in my android application. I am following documentation for Saving and retrieving, But the Sample database(Dragon) which is used in tutorial has different structure than my database.

This is my code for Pushing the data to firebase

Firebase myFirebaseRef = new Firebase("https://myfirebaseurl.firebaseio.com/android/saving-data/fireblog");
                User userName = new User(socialNum, name, datofBirth, mob1, mob2, healthCondition);
                Firebase usersRef = myFirebaseRef.child(name);
                Map<String, User> users = new HashMap<String, User>();
                users.put(name, userName);                    
              myFirebaseRef.push().setValue(users);

which create database format like this

{
      "android" : {
        "saving-data" : {
          "fireblog" : {
            "-JiRtkpIFLVFNgmNBpMj" : {
              "Name" : {
                "birthDate" : "100",
                "fullName" : "Name",
                "healthCond" : "fyhft",
                "mob1" : "5855",
                "mob2" : "5858",
                "socialNumber" : "100"
              }
            },
            "-JiRv0RmHwWVHSOiZXiN" : {
              "mast" : {
                "birthDate" : "100",
                "fullName" : "mast",
                "healthCond" : "fyhft",
                "mob1" : "5855",
                "mob2" : "5858",
                "socialNumber" : "100"
              }
            }
          }
        }
      }
    }

I want to Retrieve data from firebase such that, if I put "full Name" in my apps search box, it should retrieve that specific node, so that I can populate that information in Listview.

This is How I am trying to retrieve,

final String Find = find.getText().toString();   //Get text for search edit text box
                Firebase myFirebaseRef = new Firebase("https://myfirebaseurl.firebaseio.com/android/saving-data/fireblog");
                Query queryRef = myFirebaseRef.orderByChild("fullName");
               // System.out.println(dataSnapshot.getKey() + "is" + value.get("socialNumber"));
                System.out.println(Find);

                queryRef.addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(DataSnapshot dataSnapshot, String previousChild) {
                        System.out.println(dataSnapshot.getValue());
                        Map<String,Object> value = (Map<String, Object>) dataSnapshot.getValue();

                        String name1 = String.valueOf(value.get("fullName"));
                    //System.out.println(dataSnapshot.getKey() + "is" + value.get("fullName").toString());
                    if (name1.equals(Find)){
                        System.out.println("Name" + value.get("fullName"));
                    }
                    else{
                        System.out.println("its is null");
                    }

                    }

but It returns all the nodes,

02-19 12:18:02.053    8269-8269/com.example.nilesh.firebasetest I/System.out﹕ name
02-19 12:18:05.426    8269-8269/com.example.nilesh.firebasetest I/System.out﹕ {Name={socialNumber=100, birthDate=100, fullName=Name, mob1=5855, mob2=5858, healthCond=fyhft}}
02-19 12:18:05.426    8269-8269/com.example.nilesh.firebasetest I/System.out﹕ its is null
02-19 12:18:05.426    8269-8269/com.example.nilesh.firebasetest I/System.out﹕ {mast={socialNumber=100, birthDate=100, fullName=mast, mob1=5855, mob2=5858, healthCond=fyhft}}
02-19 12:18:05.426    8269-8269/com.example.nilesh.firebasetest I/System.out﹕ its is null

How can i Retrieve specific node so that If I enter fullName = mast, it should retrieve only second node with all the fields in that node.

like image 457
gooner Avatar asked Feb 19 '15 08:02

gooner


People also ask

How do I get particular data from Firebase?

With Firebase database queries, you can selectively retrieve data based on various factors. To construct a query in your database, you start by specifying how you want your data to be ordered using one of the ordering functions: orderByChild() , orderByKey() , or orderByValue() .

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.

How do I read data from Firebase Realtime Database?

Reading into Database To read data at a path and to listen for any changes if the data changes, we have to use the addValueEventListener() or addListenerForSingleValueEvent() method to add a ValueEventListener to a database reference. These two methods will add a valueEventListener to a DatabaseReference.

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 .


2 Answers

You're creating a query in this line:

Query queryRef = myFirebaseRef.orderByChild("fullName");

Like that the query orders the child nodes by their fullName value. But it doesn't yet limit what child nodes are returned.

To limit the nodes, you also have to filter. e.g.:

Query queryRef = myFirebaseRef.orderByChild("fullName").equalTo("gooner");

You can also get a range of nodes, by filtering with startAt and/or endAt instead of equalTo.

As Kato commented:

It looks like there is a superfluous level of children. The data is structured as saving-data/fireblog/<record id>/fluff/...actual data... and the fluff layer needs to be removed for those queries to work. You can't query children of children.

like image 136
Frank van Puffelen Avatar answered Sep 17 '22 21:09

Frank van Puffelen


If you want to get value of specif node or child node like this

enter image description here

Here if you want to get child node(address) value. You can get it in this way

    FirebaseDatabase database;
    DatabaseReference myRef;
    myRef = database.getReference();
    final DatabaseReference orders_Reference = myRef.child("Order");


 orders_Reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot data : dataSnapshot.getChildren()) {
                if(data.getKey().equals("address")){
                String orderNumber = data.getValue().toString();
                Log.d("Specific Node Value" , orderNumber);
               }
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
like image 21
Sheharyar Ejaz Avatar answered Sep 19 '22 21:09

Sheharyar Ejaz