验证用户身份后,从firebase数据库中检索用户名并将其显示在导航抽屉标题上

时间:2017-01-29 14:15:09

标签: android firebase firebase-realtime-database

在对用户进行身份验证后,从firebase数据库中检索用户名并将其显示在导航抽屉标题上。这是我的抽象活动,这里我也尝试了数据快照,但它在导航标题中返回null! 它说欢迎null。

没有像user.getNamefromDatabase()这样的方法;

package com.example.dell.Swing;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.appevents.AppEventsLogger;
import com.facebook.login.LoginManager;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.squareup.picasso.Picasso;

 public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {



private FirebaseAuth firebaseAuth;
private TextView textView;
private Button Logout;
private DatabaseReference databaseReference;

private Profile profile;
private ImageView profilePictureImageView;
private TextView userNameTextView;
// private TextView welcomeText;
// private Button logout;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FacebookSdk.sdkInitialize(getApplicationContext());
    AppEventsLogger.activateApp(this);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (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.setDrawerListener(toggle);
    toggle.syncState();

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



    firebaseAuth = FirebaseAuth.getInstance();
    profile = Profile.getCurrentProfile();

    if( firebaseAuth.getCurrentUser() == null && profile == null){
        finish();
        startActivity(new Intent(getApplicationContext(),LoginActivity2.class));
        finish();
        return;
    }


    if(profile == null){
        FirebaseUser user= firebaseAuth.getCurrentUser();
        userNameTextView = (TextView) navigationView.getHeaderView(0).findViewById(R.id.nav_user_name);
        userNameTextView.setText("Welcome"+user.getEmail());
    }else{
        profilePictureImageView = (ImageView)navigationView.getHeaderView(0).findViewById(R.id.imageView);
        userNameTextView =(TextView)navigationView.getHeaderView(0).findViewById(R.id.nav_user_name);

        Picasso.with(this).load(profile.getProfilePictureUri(200,200)).into(profilePictureImageView);
        userNameTextView.setText(profile.getName());
    }




}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}





@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_logout) {FirebaseAuth.getInstance().signOut();
        LoginManager.getInstance().logOut();
        startActivity(new Intent(MainActivity.this,LoginActivity2.class));
        MainActivity.this.finish();


    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}



}

1 个答案:

答案 0 :(得分:1)

您必须从FirebaseUser实例中检索用户名的唯一方法。因此,您需要从firebaseAuth.getCurrentUser()检索正确的值,以便能够调用getDisplayName()来检索用户的名称,getEmail()来检索用户的电子邮件,并getPhotoUrl()来检索用户的照片网址。

实现这一目标的其他方法是将用户的值存储在FirebaseDatabase中。如:

"userdata" {
     "accountCreationDAate": "2017-01-25 22:57",
     "age" : 25,
     "username" : "myName",
     "photoUrl" : "customUrl"
......

通过这种方式,您还可以从数据库中检索用户的数据。但是,从username实例始终检索photoUrlemailFirebaseUser这些重要值更为简单。