简体   繁体   中英

How to bypass unique ID and retrieve multiple auto generated child

I want to retrieve multiple child with an auto generated child(Estab_url child). Here is my Code. All i can retrieve is one child and its random

我们的数据库Firebase

final DatabaseReference establishments = database.getReference("establishments");


  establishments.child(s2).child("Estab_url").addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                if (dataSnapshot.hasChildren()) {
                 //   String value = dataSnapshot.getValue(String.class);
                    String sd = dataSnapshot.getKey();
                    Name.setText(sd);
                }

            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

If you're looking to read the URL value for each child:

establishments.child(s2).child("Estab_url").addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        String url = dataSnapshot.child("url").getValue(String.class);
        Name.setText(url);
    }

If that is not what you're looking for, please update your question to rephrase the problem.

Update

If you'd like to catch all URLs in a list, you'd set up the list outside of the listener and then add each URL to it as it comes in:

List<String> urls = new LinkedList<String>();
establishments.child(s2).child("Estab_url").addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        String url = dataSnapshot.child("url").getValue(String.class);
        urls.add(url);
        // TODO: do something with the up-to-date list of URLs
    }

But note that you still cannot use the list outside of the listener. Any code that needs the up-to-date content of the list, will need to be called/triggered from within (in this case) onChildAdded .

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