简体   繁体   中英

How to retrieve value from firebase without getting the unique key

I am trying to retrieve an url from Firebase in Android. However, my problem is that I can't retrieve the url without also getting the unique key of the child.

The value I am trying to retrieve is the following:

Firebase 图像

Down below is the code I use to retrieve the data from firebase.

 final FirebaseDatabase database = FirebaseDatabase.getInstance();
    myRef = database.getReference().child("LoadItems");

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot child : dataSnapshot.getChildren() ){
                String key;
                key = child.getKey();

                Log.d(TAG, "onDataChange: " + key);
                Log.d(TAG, "onDataChange: " + child.getValue());
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) { }
    });

So what I am trying to get is what the url alone.

There is no need to know any key in order to get that url. So please use my following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference shoesRef = rootRef.child("LoadItems").child("Shoes");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String url = ds.getValue(String.class);
            Log.d(TAG, url);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
shoesRef.addListenerForSingleValueEvent(valueEventListener);

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