user3447215
user3447215

Reputation: 23

Firebase Android, get only one key value pair

I am using Firebase Database in Android. In my app there are three type of users. one of them is "Driver" as shown in json tree below, I want that when user sign in, it automatically gets the value from key value pair "Role" so that I can start the respective activity. is there any easy way to do it or any way to do it?

Database Structure

Upvotes: 0

Views: 1391

Answers (1)

koceeng
koceeng

Reputation: 2165

Assuming you have checked that user is logged in (by Firebase Authentication) and random key child of Driver Information is user uid, then it should be like this:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

FirebaseDatabase.getInstance().getReference("Driver Information/" + user.getUid() + "/Role")
        .addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String role = dataSnapshot.getValue(String.class);

                // do someting with role
            }

            ...
        });

Note: replace addValueEventListener with addListenerForSingleValueEvent if you want to get the data one time only and don't mind if that data get changed.

Upvotes: 1

Related Questions