Skip to content
Advertisement

firebase getting exact childs from database

i have problem to get exact childs from database and add it to the list of “teams”. is that possible? i can’t find proper way to solve this problem. please help me. image of my actual database structure i want to get only values of “2” and “3”, just list of all teams without members. As a result of my solution im getting all elements from all children. i have tried sth like this:

mDatabase.child("teams").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                teamList.clear();
                for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                    teamList.add(snapshot.getValue().toString());
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        }); 

Advertisement

Answer

Use this

    mDatabase.child("teams").addValueEventListener(new ValueEventListener() {
           
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            teamList.clear();
            for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                teamList.add(snapshot.getKey());
            }
        }
    
        @Override
        public void onCancelled(@NonNull DatabaseError error) {
    
        }
    }); 
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement