Firebase数据库对事件侦听器

时间:2017-09-24 16:26:58

标签: java android firebase firebase-realtime-database

您好,在我的Firebase数据库中,我正在尝试访问一个孩子" Chatmessages",收集所有密钥并使用它们在儿童中找到相应的密钥"用户"并收集其信息。我这样做是通过使用eventlisteners查询两次。虽然如何在loadUserlist()方法中将其应用于chatIDref。

数据库结构: Figure 1

    rootRef = FirebaseDatabase.getInstance().getReference();
    chatIdRef = rootRef.child("Chatmessages").child(last);
    eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String uid = ds.getKey();
                usersRef = rootRef.child("Users").child(uid);
                ValueEventListener eventListener = new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        String image = dataSnapshot.child("image").getValue(String.class);
                        String name = dataSnapshot.child("name").getValue(String.class);
                        Boolean onlineStatus = dataSnapshot.child("onlineStatus").getValue(Boolean.class);
                        String status = dataSnapshot.child("username").getValue(String.class);
                        Log.d("TAG", image + " / " + name + " / " + onlineStatus + " / " + status);
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {}
                };
                usersRef.addListenerForSingleValueEvent(eventListener);

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    chatIdRef.addListenerForSingleValueEvent(eventListener);

    chatIdRef.keepSynced(true);
    userlist.setHasFixedSize(true);
    userlist.setLayoutManager(new LinearLayoutManager(getContext()));
    return userView;
}

public void loadUserList() {
    FirebaseRecyclerAdapter<User, UserViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<User, UserViewHolder>(
            User.class,
            R.layout.userlist_layout,
            UserViewHolder.class,
            chatIdRef
    )

0 个答案:

没有答案