Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I read data from firebase without knowing id?

I have list of operations and each operation has a id. So how can I read it without knowing id?

The data looks like this:

database

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried to write code but it doesn’t work.

myRef.child("Анна Валерьевна").child("operations").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        String s = "";
        for (DataSnapshot ds : snapshot.getChildren()){
            if (snapshot.child("name").getValue(String.class) != null) {
                s = snapshot.child("name").getValue(String.class);
            }
            adapter.add(s);
        }
    }
    @Override
    public void onCancelled(@NonNull DatabaseError error) {
    }
});

>Solution :

Your code tries to read snapshot.child("name"), but the loop variable ds. So that should be ds.child("name").

myRef.child("Анна Валерьевна").child("operations").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        String s = "";
        for (DataSnapshot ds : snapshot.getChildren()){
            if (ds.child("name").getValue(String.class) != null) {
                s = ds.child("name").getValue(String.class);
            }
            adapter.add(s);
        }
    }
    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        throw error.toException(); // never ignore errors
    }
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading