1

How can get the child values from the Firebase Realtime Database?

Below is my architecture:

Posts
|--firebase key
   |--title: test1
   |--sub: NY
   |--country: USA
 --firebase key
   |--title: test2
   |--sub: DC
   |--country: USA
 --firebase key
   |--title: test3
   |--sub: DC
   |--country: USA
 --firebase key
   |--title: test4
   |--sub: FRA
   |--country: EU
 --firebase key
   |--title: test5
   |--sub: UK
   |--country: EU

I want to read the value of sub, but there are a total of three values like DC, but I only want to fetch it once, because what I want to achieve is that states with a value will be displayed in the RecyclerView, but like AL, AZ, AL, AR and so on, it hasn't been added yet, so it won't be displayed.

So how can I do it?

I will use this methods to get post country, so how can show the subs?

DatabaseReference countryRef = FirebaseDatabase.getInstance().getReference("Posts");
        Query query = countryRef.orderByChild("country").equalTo(country);
        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                
            }

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

            }
        });

SAWYU
  • 119
  • 8

2 Answers2

1
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference postsRef = rootRef.child("Posts");
postsRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            List<String> posts = new ArrayList<>();
            for (DataSnapshot ds : task.getResult().getChildren()) {
                String sub = ds.child("sub").getValue(String.class);
                if(!posts.contains(sub)) {
                    posts.add(sub);
                }
            }
            for (String post : posts) {
                Log.d("TAG", post);
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});

The result in the logcat will be:

NY
DC
AK

If you want to display those results in a RecyclerView in Android, you should consider using my answer from the following post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I hope just display NY, DC, AK. – SAWYU Sep 08 '21 at 13:17
  • In that case, please see my updated answer. The result in the logcat will not contain duplicates. Is it ok now? – Alex Mamo Sep 08 '21 at 13:27
  • Thx bro, but I forgot the very important things. I'd updated my context. Because I will many country information, if user choose USA then it's just show USA sub, but if user choose EU then it's need to show EU – SAWYU Sep 11 '21 at 13:07
  • I'd updated my context, can you check it? – SAWYU Sep 12 '21 at 02:18
  • If you have another issue regarding reading the data, please don't edit the actual question but post a new one using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Sep 12 '21 at 08:11
0

First take a database reference using this code

private DatabaseRefrence mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();

You can Simply get child using this code

mFirstChild = mDatabase.child("child-name");
mSecondChild = mFirstChild.child("2-child-name");
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807