简体   繁体   中英

Read a Value of a Child Firebase Database

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

在此处输入图像描述

And this is my database:

在此处输入图像描述

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM