Saami
Saami

Reputation: 1

Read a Value of a Child Firebase Database

Can anyone please help? I'm stuck, can't retrieve simple data. This is my code:

enter image description here

And this is my database:

enter image description here

Upvotes: 0

Views: 80

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

Assuming that the seller note, is a direct child of your database root, to get the value of your userType property, please use the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userTypeRef = rootRef.child("seller").child(uid).child("BasicInfo").child("userType");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String userType = dataSnapshot.getValue(String.class);
        Log.d("TAG", userType);

        if(userType.equals("buyer")) {
            //Your logic
        }
    }

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

Please note that logic regarding the value you get from the database is placed inside the callback because Firebase API's are asynchronous. For more information, please also see my answer from the following post:

Upvotes: 1

Related Questions