How to retrieve specific node from firebase database in android

前端 未结 3 1029
臣服心动
臣服心动 2020-12-28 08:36

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

相关标签:
3条回答
  • 2020-12-28 09:01

    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) {
    
                    }
                });
    
    0 讨论(0)
  • 2020-12-28 09:08

    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.

    0 讨论(0)
  • 2020-12-28 09:08

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

    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) {
            }
        });
    
    0 讨论(0)
提交回复
热议问题