How to get all child's data in firebase database?

后端 未结 2 1425
南笙
南笙 2020-11-28 05:26

I have this firebase database

and i need to get all phone numbers of users , which listener shall i use to get all childes?

Every user is added as

相关标签:
2条回答
  • 2020-11-28 05:50
          DatabaseReference ref1= FirebaseDatabase.getInstance().getReference();
          DatabaseReference ref2,ref3,ref4;    
           ref2 = ref1.child("User");
    
    
           ref2.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
    
                Userlist = new ArrayList<String>();                
                // Result will be holded Here
                for (DataSnapshot dsp : dataSnapshot.getChildren()) {
                      Userlist.add(String.valueOf(dsp.geValue())); //add result into array list
    
                    }
        /* userlist will store all values of users, then point to every userlist item 
        and get mobile numbers*/
    
    0 讨论(0)
  • 2020-11-28 05:58

    First retrieve your users datasnapshot.

    //Get datasnapshot at your "users" root node
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users");
        ref.addListenerForSingleValueEvent(
                new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        //Get map of users in datasnapshot
                        collectPhoneNumbers((Map<String,Object>) dataSnapshot.getValue());
                    }
    
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        //handle databaseError
                    }
                });
    

    Then loop through users, accessing their map and collecting the phone field.

    private void collectPhoneNumbers(Map<String,Object> users) {
    
        ArrayList<Long> phoneNumbers = new ArrayList<>();
    
        //iterate through each user, ignoring their UID
        for (Map.Entry<String, Object> entry : users.entrySet()){
    
            //Get user map
            Map singleUser = (Map) entry.getValue();
            //Get phone field and append to list
            phoneNumbers.add((Long) singleUser.get("phone"));
        }
    
        System.out.println(phoneNumbers.toString());
    }
    

    This listener will only retrieve the datasnapshot when explicitly called. Consider storing a list of numbers under an "allPhoneNumbers" node in addition to your users node. This will make your datasnapshots lighter and easier to process when collecting all numbers. If you have say hundreds of users, the "users" datasnapshot will be way too big and the "allPhoneNumbers" list will be far more efficient.

    The above code was tested on your sample database and does work. However, your phone field may need to be casted to String or int depending on your user's phone instance field's type.

    0 讨论(0)
提交回复
热议问题