有一个Firebase实时数据库,我在注册期间将一些数据放入其中,它们属于Auth UID密钥下的Users部分:
public void writeUserToDB(Object data, String currentUser){
DatabaseReference myRef = mDataBase.child("Users/" + currentUser);
myRef.setValue(data);
}
mRegisterBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id = mDataBase.getKey();
String name = mName.getText().toString().trim();
String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
String userBinder = mUserBinder.getText().toString().trim();
if (TextUtils.isEmpty(email)) {
mEmail.setError("Write your email...");
return;
}
if (TextUtils.isEmpty(password)) {
mEmail.setError("Write your password...");
return;
}
if (TextUtils.isEmpty(name)) {
mName.setError("Write your name...");
return;
}
if (password.length() < 8) {
mPassword.setError("Password must have 8+ symbols.");
return;
}
fAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
User newUser = new User(currentUser, id, name, email, password, userBinder);
currentUser = FirebaseAuth.getInstance().getCurrentUser().getUid();
writeUserToDB(newUser, currentUser);
Toast.makeText(Register.this, "Пользователь зарегистрирован.", Toast.LENGTH_LONG).show();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
} else {
Toast.makeText(Register.this, "Произошла ошибка. Повторите." +
task.getException().getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
});
在firebase中看起来像:
Users:
UID: {
email
name
password
userbinder
}
我需要得到这个数据在MainActivity作为字符串变量使用时,另一个按钮被点击。UPD -这就是我想做的,但它不起作用,执行不同的功能。
String uid = FirebaseAuth.getInstance().getUid();
mDataBase = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = mDataBase.child("Users/").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String email = ds.child("email").getValue(String.class);
String userBinder = ds.child("userBinder").getValue(String.class);
Log.d("TAG", email + "/" + userBinder);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
我需要在MainActivity按钮中使用此数据:
mSosButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatabaseReference databaseReference = FirebaseDatabase.getInstance()
.getReference().child("Sos");
Toast.makeText(MainActivity.this, "Message sended!", Toast.LENGTH_LONG).show();
Sos sos = new Sos(user_email, binded_user_email);
databaseReference.push().setValue(sos);
}
});
为了能够获得已登录用户的数据,必须附加一个精确指向Users
节点的侦听器。所以请使用以下代码行:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = db.child("Users").child(uid);
uidRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
DataSnapshot snapshot = task.getResult();
String email = snapshot.child("email").getValue(String.class);
String userBinder = snapshot.child("userBinder").getValue(String.class);
Log.d("TAG", email + "/" + userBinder);
} else {
Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
}
}
});
请注意,不需要循环遍历结果。您只需要获得对应于这两个字段的数据。