Recover user name and email in NavigationDrawer?

0

How do I retrieve the name and email of the logged-in user? It is giving code error. I am using BD Firebase.

    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) {

        }
    });

}
    
asked by anonymous 18.04.2018 / 17:52

1 answer

1

If you are using the NavigationDrawer that 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 on some node, to retrieve 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 place yours, from it I order the list where the email is equal to my email that I have already taken, so inside I can retrieve any node value inside of this user.

REMEMBER - you must have saved the user's email in your node, otherwise this query will not work, if you have not saved it, use something else that only that user has to be able to retrieve only it within% p>

In the example I made look how db is:

    
18.04.2018 / 17:57