简体   繁体   English

如何显示当前登录的用户Firebase

[英]How to Display The Current Logged In User 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);
    }
}
}
}

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. 目前,每个用户区分的方式是通过在登录时分配给他们的UID。当他们登录时,他们使用存储在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 目前它显示了用户表的详细信息,但我只希望它显示登录用户的详细信息。输出显示在ListView中

First get uid of the current user that is logged in: 首先获取登录的当前用户的uid:

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() {
public void onDataChange(DataSnapshot dataSnapshot) { 
String name=dataSnapshot.child("name").getValue().toString(); 
name1.setText(name);
}

Assuming you have this: 假设你有这个:

Users
  userid
     name: peter
     //etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM