当前用户字符串
Current user to string
我在显示数据时遇到了问题。在我使用当前用户 UID 作为密钥之前,但现在我使用自定义密钥,我不知道如何在我的代码中将此密钥作为字符串调用。
我的代码这部分 ( child(user.getUid() )
FirebaseUser userUID;
userUID = FirebaseAuth.getInstance().getCurrentUser();
final DatabaseReference NameRef = database.getReference().child("No server").child(userUID.getUid()).child("name");
final DatabaseReference LastNameRef = database.getReference().child("No server").child(userUID.getUid()).child("lastName");
final DatabaseReference EmailRef = database.getReference().child("No server").child(userUID.getUid()).child("email");
发生了什么变化
我想让用户输入一个字符串“dan ned”。
编辑:
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference NameRef = database.getReference().child("No server").child(user.getUid()).child("name");
final DatabaseReference LastNameRef = database.getReference().child("No server").child(user.getUid()).child("lastName");
final DatabaseReference EmailRef = database.getReference().child("No server").child(user.getUid()).child("email");
NameRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
final String strName = dataSnapshot.getValue(String.class);
textName.setText(strName);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
LastNameRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
final String strLastName = dataSnapshot.getValue(String.class);
textLastName.setText(strLastName);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
EmailRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
final String strEmail = dataSnapshot.getValue(String.class);
textEmail.setText(strEmail);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
无需附加三个侦听器即可读取这三个值。您只能使用一个,如以下代码行所示:
String name = FirebaseAuth.getInstance().getCurrentUser().getDisplayName();
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference nameRef = db.child("No server").child(name);
nameRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
DataSnapshot snapshot = task.getResult();
String key = snapshot.getKey();
Log.d("TAG", key); //It will print "dan ned"
String email = snapshot.child("email").getValue(String.class);
Log.d("TAG", email); //It will print "DanilNed@gmail.com"
String lastName = snapshot.child("lastName").getValue(String.class);
Log.d("TAG", lastName); //It will print "Ned"
String name = snapshot.child("name").getValue(String.class);
Log.d("TAG", name); //It will print "Dan"
} else {
Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
}
}
});
我在显示数据时遇到了问题。在我使用当前用户 UID 作为密钥之前,但现在我使用自定义密钥,我不知道如何在我的代码中将此密钥作为字符串调用。
我的代码这部分 ( child(user.getUid() )
FirebaseUser userUID;
userUID = FirebaseAuth.getInstance().getCurrentUser();
final DatabaseReference NameRef = database.getReference().child("No server").child(userUID.getUid()).child("name");
final DatabaseReference LastNameRef = database.getReference().child("No server").child(userUID.getUid()).child("lastName");
final DatabaseReference EmailRef = database.getReference().child("No server").child(userUID.getUid()).child("email");
发生了什么变化
我想让用户输入一个字符串“dan ned”。
编辑:
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference NameRef = database.getReference().child("No server").child(user.getUid()).child("name");
final DatabaseReference LastNameRef = database.getReference().child("No server").child(user.getUid()).child("lastName");
final DatabaseReference EmailRef = database.getReference().child("No server").child(user.getUid()).child("email");
NameRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
final String strName = dataSnapshot.getValue(String.class);
textName.setText(strName);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
LastNameRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
final String strLastName = dataSnapshot.getValue(String.class);
textLastName.setText(strLastName);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
EmailRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
final String strEmail = dataSnapshot.getValue(String.class);
textEmail.setText(strEmail);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
无需附加三个侦听器即可读取这三个值。您只能使用一个,如以下代码行所示:
String name = FirebaseAuth.getInstance().getCurrentUser().getDisplayName();
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference nameRef = db.child("No server").child(name);
nameRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
DataSnapshot snapshot = task.getResult();
String key = snapshot.getKey();
Log.d("TAG", key); //It will print "dan ned"
String email = snapshot.child("email").getValue(String.class);
Log.d("TAG", email); //It will print "DanilNed@gmail.com"
String lastName = snapshot.child("lastName").getValue(String.class);
Log.d("TAG", lastName); //It will print "Ned"
String name = snapshot.child("name").getValue(String.class);
Log.d("TAG", name); //It will print "Dan"
} else {
Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
}
}
});