0

There is a User object, and from the given collection you want to output all the numbers. In order to compare the uniqueness of the number. The data can be output to the console. But it cannot be added to the ArrayList.

enter image description here

     public DatabaseReference databaseReference;
     ArrayList<String> codesOfUsers = new ArrayList<>();

    databaseReference = FirebaseDatabase.getInstance().getReference().child("User");
    ChildEventListener сhildEventListener = new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

                String number = snapshot.child("number").getValue(String.class);
                codesOfUsers.add(number);
                System.out.println(number);
        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot snapshot) {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

        }

        @Override
        public void onCancelled(@NonNull DatabaseError  error) {
            Log.e("The read failed: " ,error.getMessage());
        }
    };

    databaseReference.addChildEventListener(сhildEventListener);
    System.out.println("Output" + codesOfUsers.toString());
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 2
    This is an asynchronous call to the database. Your final `println` is therefore being called before any data has been returned. Is this the issue? – codebod May 17 '22 at 19:24
  • Does this answer your question? [Why does my function that calls an API return an empty or null value?](https://stackoverflow.com/a/70178210/9473786) – Tyler V May 17 '22 at 23:12

0 Answers0