Chloe Hamilton
Chloe Hamilton

Reputation: 95

How to retrieve a username from firebase realtime database

I have a firebase realtime db set up with a node for ratings and a node for users. Each user has their unique key, within which their details are found including location, username etc.

For a welcome back screen, I want to display the username. The user logs in with their email and password. I can get the email to display pretty easily, but the username is a bit trickier.

I'm trying to use a transaction to get the value of the username and return it.

package com.example.securityapp;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.MutableData;
import com.google.firebase.database.Transaction;

public class home extends AppCompatActivity {

    DatabaseReference databaseUsers;
    Button training, leaderboard;
    Button signOut;
    TextView welcome;
    Button loginPage;
    private FirebaseAuth mAuth;
    FirebaseDatabase database = FirebaseDatabase.getInstance();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);


        databaseUsers = database.getReference("Users");
        leaderboard = (Button) findViewById(R.id.leaderboard);
        training = (Button) findViewById(R.id.training_button);
        signOut = (Button) findViewById(R.id.sign_out);
        mAuth = FirebaseAuth.getInstance();
        welcome = (TextView) findViewById(R.id.welcome_message);
        welcome.setText("Welcome back " + mAuth.getCurrentUser().getEmail());



    }
    public void getUsername() {

        String id = mAuth.getCurrentUser().getUid();
        DatabaseReference username = databaseUsers.child(id).child("username");

        username.runTransaction(new Transaction.Handler() {
            @Override
            public Transaction.Result doTransaction(MutableData mutableData) {
                String name = mutableData.getValue(String.class);
                System.out.println("The username is: " + name);
                return Transaction.success(mutableData);
            }

            @Override
            public void onComplete(@Nullable DatabaseError databaseError, boolean b, @Nullable DataSnapshot dataSnapshot) {

            }

        });


    }

This is what my users node looks like in my db:

enter image description here

Upvotes: 2

Views: 2860

Answers (1)

Md. Asaduzzaman
Md. Asaduzzaman

Reputation: 15423

You can use ListenerForSingleValueEvent like below to get username

FirebaseDatabase database = FirebaseDatabase.getInstance();
databaseUsers = database.getReference("Users");
String id = mAuth.getCurrentUser().getUid();
DatabaseReference username = databaseUsers.child(id).child("username");

username.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        String username = dataSnapshot.getValue().toString();
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

Beside this you can still use runTransaction and inside onComplete you can get username like below:

username.runTransaction(new Transaction.Handler() {
    @NonNull
    @Override
    public Transaction.Result doTransaction(@NonNull MutableData mutableData) {
        String name = mutableData.getValue(String.class);
        return Transaction.success(mutableData);
    }

    @Override
    public void onComplete(@Nullable DatabaseError databaseError, boolean b, @Nullable DataSnapshot dataSnapshot) {
        String username = dataSnapshot.getValue(String.class);
        if(username != null)
            welcome.setText("Welcome back " + username);
    }
});

Upvotes: 2

Related Questions