0
    String currentUserID = Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid();
    dataRef = FirebaseDatabase.getInstance().getReference();

    //lấy dữ liệu chats của người dùng về
    dataRef.child("Chats").child(currentUserID).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot snapshot: dataSnapshot.getChildren()){
                for (DataSnapshot snapshot1 : snapshot.getChildren()){
                    Chats chat = snapshot1.getValue(Chats.class);  //I got chat here
                    Chat.add(chat);                                // and I save it into ArrayList 
                }
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

    //Lấy ra các ID của nhưng người đã nhắn tin để đoạn dưới lấy tên -_-
  ArrayList<String> listIdUserChat = new ArrayList<>();
    for(Chats chat: Chat){                                          //But here ArrayList Chat is null
       String ID = chat.getFrom();
      if(!ID.equals(currentUserID)){
           if(!listIdUserChat.contains(ID)) {
                listIdUserChat.add(ID);
            }
       }
    }

.............

When i debug

Why is it like that and how do I fix it? Sorry because my English not good and I'm an amateur. Thanks all.

ND_Tuong
  • 1
  • 1
  • `addValueEventListener` is an asynchronous call i.e it will run on separate thread. Move your code inside `onDataChange` .. – ADM May 01 '20 at 02:51
  • Firebase API is asynchronous. So please check the duplicate to how can you solve this using a custom callback. – Alex Mamo May 01 '20 at 06:42

0 Answers0