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

查看:79
本文介绍了如何显示当前已登录用户的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天全站免登陆