Skip to content Skip to sidebar Skip to footer

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 differe

Solution 1:

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.


Solution 2:

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) {
        }
    });

Solution 3:

This is another way to retrieve single value from firebase database using Hashmap.

     ArrayList<HashMap<String,String>> AllUserscourselist;
 String SelectedCourseUserUId;
     DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();

     databaseReference.child("User_course_Details").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
    //                        emergencyContactsList = new ArrayList<>();
                    AllUserscourselist = new ArrayList<HashMap<String, String>>();
                    if(dataSnapshot.exists())
                    {
                        for(DataSnapshot postSnapShot:dataSnapshot.getChildren())
                        {

                            for (DataSnapshot courseUID : postSnapShot.getChildren()){
                                UsercourseDetails usercourseDetails = courseUID.getValue(UsercourseDetails.class);
                                HashMap<String, String> map = new HashMap<String, String>();

                                String user_id = postSnapShot.getKey();
                                String course_id = usercourseDetails.getcourse_id();
                                String course_type = usercourseDetails.getcourse_type();
                                String course_brand = usercourseDetails.course_brand;
                                String course_number_plate_url = usercourseDetails.course_number_plate_url;

                                map.put("user_id", user_id);
                                map.put("course_id", course_id);
                                map.put("course_type", course_type);
                                map.put("course_brand", course_brand);
                                map.put("course_number_plate_url", course_number_plate_url);

                                AllUserscourselist.add(map);
                            }

    //                        AllUserscourselist.add(new UsercourseDetails(usercourseDetails.getcourse_type(),usercourseDetails.getcourse_brand(),usercourseDetails.getcourse_number_plate_url(),usercourseDetails.getcourse_id()));
                        }

                        Log.e("AllUserscourselist",""+AllUserscourselist);

                        courseIdList = new ArrayList<String>();
                        for (int i = 0; i < AllUserscourselist.size(); i++) {
                            String course_id_list;
                            course_id_list = AllUserscourselist.get(i).get("course_id")+" "+ AllUserscourselist.get(i).get("user_id");
                            courseIdList.add(course_id_list);
                        }

                        if(courseIdList.size()>0 && courseIdList!=null) {
                            for (int i = 0; i < courseIdList.size(); i++) {   //used
                                String arr[] = courseIdList.get(i).split(" ");
                                if (arr[0].equals(coursenumber)) {
                                    SelectedcourseUserUId = arr[1];
                                    getUserEmergencyContacts(SelectedcourseUserUId);
                                    break;
                                }
                            }
                        }

                    }else{
    //                    NavigationActivity.this.overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_in_left);
                    }
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

Solution 4:

Instead of getting all the nodes and then iterating it to get nodes based on value, just trigger the query provided by firebase.

   private void readAllRequestedFormFromFirebaseDb(){
    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference ref = database.getReference("App_DB");
    DatabaseReference childRef = ref.child("AllRequest");
    Query queryRef = 
  childRef.orderByChild("fullName").equalTo("Jay");
    queryRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            // getting list
            for(DataSnapshot dataSnapshot: snapshot.getChildren()){
                QueryFormModel post = dataSnapshot.getValue(QueryFormModel.class);
                queryFormModelArrayList.add(post);
                /*the above list will have record only
                 with the provided fullName as Jay*/

            }

        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Log.e("DetailsActivity", "onCancelled:readAllSubmittedRequestFromFirebaseDb:  "+databaseError );
        }
    });
}

Click here and find the beautifully written post about this topic


Post a Comment for "How To Retrieve Specific Node From Firebase Database In Android"