Home > Software engineering >  Call all values of a specific sub-child in firebase
Call all values of a specific sub-child in firebase

Time:07-10

This is my firebase realtime database enter image description here

I am trying to call all values of "Deadline" but I can't make it work.

I tried

DatabaseReference databaseDeadline = FirebaseDatabase.getInstance().getReference("subjects");
DatabaseReference databaseDeadline1 = databaseDeadline.child("MMW");

and

DatabaseReference databaseDeadline = FirebaseDatabase.getInstance().getReference("subjects");
DatabaseReference databaseDeadline1 = databaseDeadline.child("MMW");
DatabaseReference databaseDeadline2 = databaseDeadline1.child("");

and

DatabaseReference databaseDeadline = FirebaseDatabase.getInstance().getReference("subjects").child("MMW");

and

DatabaseReference databaseDeadline = FirebaseDatabase.getInstance().getReference("subjects").child("MMW").child("");

then I test here to see if it works but it always return null no matter what. How can i make it work? Thank you in advance

*db_reference*.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                String dbDeadline = snapshot.child("Deadline").getValue(String.class);
                if (dbDeadline != null){
                    Toast.makeText(AssignmentListMmw.this, "I exist",
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(AssignmentListMmw.this, "XXX",
                            Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

CodePudding user response:

To read the value from subjects/MMW/B/Deadline, you'd do:

DatabaseReference db_reference = FirebaseDatabase.getInstance().getReference("subjects/MMW/B/Deadline");
db_reference.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        String dbDeadline = snapshot.getValue(String.class);
        System.out.println(dbDeadline);
    }
    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        throw error.toException(); // Never ignore errors
    }
});

If neither your onDataChange nor the onCancelled is getting called, you may not be connected to the database correctly. In that case, consider redownloading your google-services.json or specifying the database URL in the call to getInstance(...) as shown here: unable to connect to firebase realtime database andriod studio.


If you want to read all Deadline values under subjects/MMW, you can do:

DatabaseReference db_reference = FirebaseDatabase.getInstance().getReference("subjects/MMW");
db_reference.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        for (DataSnapshot child: snapshot.getChildren()) {
            String dbDeadline = child.child("Deadline").getValue(String.class);
            System.out.println(dbDeadline);
        }
    }
    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        throw error.toException(); // Never ignore errors
    }
});
  • Related