0

I am retrieving data of blocked users from firebase, and the arrayList has data in the advaluevent listener but after calling advaluevent listener ,the arrayList becomes empty .I dont know what is the problem?

    showUserClassArrayList=new ArrayList<>();


    DatabaseReference databaseReference=FirebaseDatabase.getInstance().getReference("blocked").child(FirebaseAuth.getInstance().getUid());
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
            if (snapshot.exists()){



                   showUserClassArrayList.clear();
                for(DataSnapshot dataSnapshot: snapshot.getChildren()){
                    showUserClass=dataSnapshot.getValue(ShowUserClass.class);
                    showUserClassArrayList.add(showUserClass);

                    Toast.makeText(getActivity(), String.valueOf(showUserClassArrayList.size()), Toast.LENGTH_SHORT).show();

                }


            }
        }

        @Override
        public void onCancelled(@NonNull @NotNull DatabaseError error) {

        }
    });
Bhoomika Patel
  • 1,895
  • 1
  • 13
  • 30
  • 1
    Does this answer your question? [Why does my function that calls an API return an empty or null value?](https://stackoverflow.com/questions/57330766/why-does-my-function-that-calls-an-api-return-an-empty-or-null-value). The code in the listener is asynchronous, it does not run in the order written so if you check the array right after adding the listener it will always be empty. The listener code where you add to the array runs much later in the future when it actually gets the data. – Tyler V May 24 '22 at 04:35
  • Put your Toast outside from the loop – Ticherhaz FreePalestine May 24 '22 at 04:39

0 Answers0