user9266642
user9266642

Reputation:

Retrieve data under userids from firebase

I'm facing a problem on how to retrieve all data under uids to list string.But I don't know how to pass uids.

Here is the sample database structure.The red one is the uid of each user.The green one is the random push key. Edit I want to retrieve all data ..I mean there can have many uids and their childs.I want to access all uid(not only my uid but also other uids under mdg node).😪 Please help me...

Upvotes: 0

Views: 75

Answers (1)

Ali Ahmed
Ali Ahmed

Reputation: 2178

Create a global ArrayList in your Activity

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

You need Model class for storing all data.

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..

}

Then in your 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) {

        }
    });

Upvotes: 1

Related Questions