简体   繁体   中英

How to fetch child names in Firebase realtime database

This is my Firebase database. I want to fetch only the child names of Intake-Sec like CSE, EEE . Not the children of CSE or EEE . How can I do this?

在此处输入图片说明

Thanks in advance!

To get only the CSE and EEE , please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> list = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String key = ds.getKey();
            list.add(key);
            Log.d(TAG, key);
        }

        //Do what you need to do with your list
    }

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

The list will contains only two elements as it will also be printed in the logcat:

CSE
EEE

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