从Firebase检索用户ID下的数据

时间:2018-11-29 10:01:28

标签: android firebase firebase-realtime-database

我在如何检索uids下的所有数据以列出字符串时遇到问题。但是我不知道如何传递uids。

Here is the sample database structure.The red one is the uid of each user.The green one is the random push key. 修改 我想检索所有数据..我的意思是可以有很多uid和他们的孩子。我想访问所有uid(不仅是我的uid,而且是mdg节点下的其他uid)。请帮助我...

1 个答案:

答案 0 :(得分:1)

在您的ArrayList中创建一个全局Activity

private ArrayList<User> arrayList = new ArrayList<>();

您需要Model类来存储所有数据。

class User {

    private String postText, uploadTime , Uplaoder;


    public User(String postText, String uploadTime, String uplaoder) {
        this.postText = postText;
        this.uploadTime = uploadTime;
        Uplaoder = uplaoder;
    }

    //getter setter here..

}

然后在您的Activity

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("msg");

    reference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot snapshotMessages : dataSnapshot.getChildren()) {
                for (DataSnapshot snapshot : snapshotMessages.getChildren()) {

                    String post_text = snapshot.child("post_text").getValue(String.class);
                    String upload_time = snapshot.child("upload_time").getValue(String.class);
                    String uploader_name = snapshot.child("uploader_name").getValue(String.class);

                    User user = new User(post_text, upload_time, uploader_name);

                    arrayList.add(user);

                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });