如何显示当前登录的用户 Firebase [英] How to Display The Current Logged In User Firebase

查看:21
本文介绍了如何显示当前登录的用户 Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    mAuth = FirebaseAuth.getInstance();
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    myRef = mFirebaseDatabase.getReference().child("Users");
    FirebaseUser user = mAuth.getCurrentUser();
    userID = user.getUid();

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
                toastMessage("Successfully signed out.");
            }
            // ...
        }
    };

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.
            showData(dataSnapshot);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

/*private void showData(DataSnapshot dataSnapshot) {
    for(DataSnapshot ds : dataSnapshot.getChildren()){
        UserInformation uInfo = new UserInformation();
        uInfo.setName(ds.child(userID).getValue(UserInformation.class).getName()); //set the name
        uInfo.setHandicap(ds.child(userID).getValue(UserInformation.class).getHandicap()); //set the name
        uInfo.setAge(ds.child(userID).getValue(UserInformation.class).getAge()); //set the email
        uInfo.setGender(ds.child(userID).getValue(UserInformation.class).getGender()); //set the phone_num

        //display all the information
        Log.d(TAG, "showData: name: " + uInfo.getName());
        Log.d(TAG, "showData: age: " + uInfo.getAge());
        Log.d(TAG, "showData: handicap: " + uInfo.getHandicap());
        Log.d(TAG, "showData: gender: " + uInfo.getGender());

        ArrayList<String> array  = new ArrayList<>();
        array.add("Full Name:");
        array.add(uInfo.getName());
        array.add("Age:");
        array.add(uInfo.getAge());
        array.add("Handicap:");
        array.add(uInfo.getHandicap());
        array.add("Gender:");
        array.add(uInfo.getGender());
        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
        mListView.setAdapter(adapter);
    }
}
*/
private void showData(DataSnapshot dataSnapshot) {
    ArrayList<String> array  = new ArrayList<>();
    for(DataSnapshot ds : dataSnapshot.getChildren()){
        UserInformation uInfo = ds.getValue(UserInformation.class);
        array.add(" Full Name /  " +uInfo.getName());
        array.add(" Age /  " + uInfo.getAge());
        array.add(" Handicap/ " + uInfo.getHandicap());
        array.add(" Gender/ " + uInfo.getGender());


    }
    ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
    mListView.setAdapter(adapter);
}

@Override
public void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
}

@Override
public void onStop() {
    super.onStop();
    if (mAuthListener != null) {
        mAuth.removeAuthStateListener(mAuthListener);
    }
}
}
}

目前区分每个用户的方式是通过登录时分配给他们的 UID.当他们登录时,他们使用存储在 Firebase 身份验证部分的用户名和密码.当用户通过验证后,他们将被定向到他们的帐户页面.在帐户页面上,我然后要求用户输入他们的个人详细信息,这些信息保存在表用户下的实时数据库中.如何显示当前登录的用户信息?目前它显示用户表的详细信息,但我只希望它显示登录用户的详细信息.输出显示在 ListView 中

Currently the way each user is differentiated is by the UID which is allocated to them when they log in. When they log in they use username and password which is stored in the authentication part of Firebase. When the user has been verified they are directed to their Account Page. On the Account page I then ask the user to input their personal details which is saved in the real time database under Table Users. How do I display the current logged user information ? Currently it shows details of the user table but I only want it to show the details of the user who is logged in. The output is shown in a ListView

推荐答案

首先获取当前登录用户的uid:

First get uid of the current user that is logged in:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userid = user.getUid();

然后检索当前用户的数据:

then retrieve the data of the current user:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
ref.child(userid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) { 
String name = dataSnapshot.child("name").getValue().toString(); 
name1.setText(name);
}

假设你有这个:

Users
  userid
     name: peter
     //etc

这篇关于如何显示当前登录的用户 Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆