简体   繁体   中英

How do I retrieve a list of all children in a Firebase realtime database?

The structure of my Firebase database is shown below. I want to make a list of all the Rooms in Android Studio. In other words, I need an array of [Room1, Room2, ...] for use in a spinner. How can I do this?

在此处输入图片说明

What you can do is loop over all the children of your database and add them to an ArrayList and then use an array adapter to display the list.

What I am saying, looks something like this in code:

rootRef.child("Rooms").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
       for (DataSnapshot ds : dataSnapshot.getChildren()) {
            array.add(ds.child("Users").child("UserID").getValue(String.class));
        }

        ArrayAdapter adapter = new ArrayAdapter(YourActivity.this, android.R.layout.simple_list_item_1, array);
        listView.setAdapter(adapter);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        // Do something for errors too
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM