0

I'm trying to display the name of my current user inside an menu item, I am getting the username like this

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
DatabaseReference name = 
ref.child("Users").child(mCurrentUser.getUid()).child("name");

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

username2 = username; //should i be doing this? how can i pass username 
                      //directly to other other methods in the code?

}

and it works fine because i have tested it with toasts, however when i pass username2 to the setTitle method it does not display it. If i pass anything else inside it, it works just fine. If i pass username2 + something else it displays nullsomething else

NavigationView navigationView = (NavigationView) 
findViewById(R.id.navmenuffs);
Menu menu = navigationView.getMenu();
MenuItem nav_profile = menu.findItem(R.id.nav_profile);
nav_profile.setTitle(username2);

username2 is just a String I have declared public so i can use username in other methods

  • In which part of your code are you using this line of code `nav_profile.setTitle(username2);`? – Alex Mamo Aug 05 '19 at 07:02
  • If you call this `nav_profile.setTitle(username2);` before `onDataChange` and `username2` has no default value, of course it wont display anything. – faranjit Aug 05 '19 at 09:13

0 Answers0