Recover user name and email in Navigationdrawer?

Asked

Viewed 337 times

0

How do I recover the name and email of the logged-in user? I’m using Firebase BD.inserir a descrição da imagem aqui

    public class MenuActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

private Toolbar toolbar;
private TextView nomePefil, emailPerfil;
private FirebaseAuth autenticacao;
private DatabaseReference firebase;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    //Recuperar Nome e E-mail para o Menu

    nomePefil = findViewById(R.id.tv_nomePerfil);
    emailPerfil = findViewById(R.id.tv_emailPerfil);

    autenticacao = FirebaseAuth.getInstance();
    String email = autenticacao.getCurrentUser().getEmail();

    //>>>>>>>> erro aqui <<<<<<<<<<
    emailPerfil.setText(email);

    firebase = FirebaseDatabase.getInstance().getReference();
    firebase.child("usuarios").orderByChild("email").equalTo(email).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapShot : dataSnapshot.getChildren()){
                String nome = postSnapShot.child("nome").getValue().toString();
                nomePefil.setText(nome);
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

1 answer

1


If you’re using the Navigationdrawer the template already provides:

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

navigationView.setNavigationItemSelectedListener(this);

    View headerView = navigationView.getHeaderView(0);

    // Obtém a referência dos TextViews a partir do NavigationView 
    TextView text1 = (TextView) headerView.findViewById(R.id.text1);
    TextView text2 = (TextView) headerView.findViewById(R.id.text2);

    text1.setText("meu_texto");
    text2.setText("meu_texto");

To recover from firebase do:

private FirebaseAuth autenticacao;
autenticacao = FirebaseAuth.getInstance();
String email = autenticacao.getCurrentUser().getEmail();

For the name, it must be saved in some node, to recover the values you do so:

private DatabaseReference referenciaFirebase;
 referenciaFirebase = FirebaseDatabase.getInstance().getReference();

//Em "usuarios" eu ordeno tudo pelo email onde o email for igual ao meu email, então aí eu tenho todo o meu nó a disposição para recuperar os dados
referenciaFirebase.child("usuarios").orderByChild("email").equalTo(email).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for(DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                        String nome = postSnapshot.child("nome").getValue().toString();


                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

"Users" is the name of the main node, you put yours, from it I order the list where the email is equal to my email I already took, so inside the for I can recover any node value within that user.

REMEMBERING - you must have saved in your node the user’s email, otherwise this query will not work, if you have not saved, use something that only that user has to be able to recover only it inside the for

In the example I did look like db:

inserir a descrição da imagem aqui

  • I did exactly as you said but is giving an error forwarding to : emailPerfil.setText(email); when you click on the Run console. I posted the code above.

  • The user is already logged in?

  • If not will return null

  • Yes. You are logged in.

  • Log the email variable, see if it is null, if it is, the user is not logged in

  • No need to convert getEmail to String?

  • Actually no, it already returns a string, but if you want to put the toString(), feel free

  • It’s gonna work out

  • I discovered your mistake, you referenced the textview in the wrong way, in my example first I create a view and from it I make the reference, look at it up as I did

  • When setting text, your textview has a null reference, not the null text value

  • You can look there, are the first lines of the answer

  • worked well friend. Thanks for your attention!

  • ;) d nothing, we need there

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.