如何使用Firebase在Android工作室项目中检索已记录的用户数据以导航标题作为用户名

我是Android Project的新手。我使用Firebase创建项目。在这个项目中,我想将记录的用户名添加到导航标题中的文本视图中。我用于登录或注册模型选项。但我不能。有人对此有任何经验吗?我的火库数据库数据如下所示。

http://img3.mukewang.com/62fc4cb7000164de03610152.jpg

我想做成这样

http://img1.mukewang.com/62fc4cc50001f47202690543.jpg

我的家庭活动是这样的


package com.cavad.firebaseproject;


import android.os.Bundle;

import android.support.annotation.NonNull;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentTransaction;

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.Menu;

import android.view.MenuItem;



public class Home extends AppCompatActivity

    implements NavigationView.OnNavigationItemSelectedListener {



@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_home);

    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.addDrawerListener(toggle);

    toggle.syncState();


    NavigationView navigationView = (NavigationView) 

 findViewById(R.id.nav_view);

    navigationView.setNavigationItemSelectedListener(this);


    Fragment category = new CategoryFragment();

    this.setDefaultFragment(category);

 }


 private void setDefaultFragment(Fragment defaultFragment)

 {

    this.replaceFragment(defaultFragment);

 }


 public void replaceFragment(Fragment destFragment)

 {

    FragmentManager fragmentManager = this.getSupportFragmentManager();

    FragmentTransaction fragmentTransaction = 

 fragmentManager.beginTransaction();

    fragmentTransaction.replace(R.id.category, destFragment);

    fragmentTransaction.commit();

 }



梵蒂冈之花
浏览 102回答 3
3回答

BIG阳

步骤 1.获取当前登录用户:-获取当前用户的推荐方法是调用 getCurrentUser 方法。如果没有用户登录,getCurrentUser 将返回 null。示例 :-FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();if (user != null) {// Name, email address, and profile photo UrlString name = user.getDisplayName();String email = user.getEmail();Uri photoUrl = user.getPhotoUrl();// Check if user's email is verifiedboolean emailVerified = user.isEmailVerified();// The user's ID, unique to the Firebase project. Do NOT use this value to// authenticate with your backend server, if you have one. Use// FirebaseUser.getIdToken() instead.String uid = user.getUid();}有关详细信息,请查看链接在 Firebase 中管理用户步骤2.将其设置为导航视图标题:-你可以得到文本视图,如:-NavigationView navigationView = (NavigationView) findViewById(R.id.your_nav_view_id);View header = navigationView.getHeaderView(0)TextView text = (TextView) header.findViewById(R.id.textView);

狐的传说

从导航标题中的 firebase 实时数据库中检索记录的用户数据xml 文件&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/your_name"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:fontFamily="@font/gotham_bold"&nbsp; &nbsp; &nbsp; &nbsp; android:text="User Name"&nbsp; &nbsp; &nbsp; &nbsp; android:textColor="@color/brown"&nbsp; &nbsp; &nbsp; &nbsp; android:textSize="18sp"&nbsp; &nbsp; &nbsp;/>&nbsp; &nbsp; &nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/user_name"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:fontFamily="@font/gotham_book"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="User Name"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textColor="@color/brown"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:textSize="15sp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_gravity="center"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />java 代码&nbsp; &nbsp; firebaseDatabase.getReference().child("Users").child(auth.getUid())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addListenerForSingleValueEvent(new ValueEventListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onDataChange(@NonNull DataSnapshot snapshot) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; User user = snapshot.getValue(User.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View header = navigationView.getHeaderView(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name = header.findViewById(R.id.your_name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; username = header.findViewById(R.id.user_name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name.setText(user.getName1());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; username.setText("@" + user.getUsername1());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onCancelled(@NonNull DatabaseError error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });

蓝山帝景

如果您想检索存储在实时数据库中的已登录用户电子邮件,用户名和图片,并在导航标题中显示它们,那么您可以简单地按照以下操作:&nbsp; &nbsp; NavigationView navigationView = findViewById(R.id.nav_view);&nbsp; &nbsp; &nbsp; &nbsp; database = FirebaseDatabase.getInstance();&nbsp; &nbsp; &nbsp; &nbsp; auth = FirebaseAuth.getInstance();&nbsp; &nbsp; database.getReference().child("Users").child(auth.getUid())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addListenerForSingleValueEvent(new ValueEventListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onDataChange(@NonNull DataSnapshot snapshot) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //we have Users class which contain user properties and methods.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; User user = snapshot.getValue(User.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View header = navigationView.getHeaderView(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nav_email = header.findViewById(R.id.nav_email);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nav_username = header.findViewById(R.id.nav_username);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nav_profile = header.findViewById(R.id.nav_profilePic);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Picasso.get().load(user.getProfilePic()).placeholder(R.drawable.avatar).into(nav_profile);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nav_email.setText(user.getMail());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nav_username.setText(user.getUserName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onCancelled(@NonNull DatabaseError error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });如果要使用 Picasso 从实时数据库中检索图像,然后在导航标头图像视图上设置它,则在捆绑包 Gradle 模块文件中添加以下依赖项:&nbsp; &nbsp; implementation 'com.squareup.picasso:picasso:2.71828'然后,您可以在项目中使用毕加索。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java