I am trying to get values from Firebase
Database and set them to text view. My database has multiple child like in image. I want to get the Phone and Address values from it. How can I get them? I am new to Android and I have tried multiple answers here, but failed. Can anyone help me with this?
我正在尝试从Firebase数据库获取值并将其设置为文本视图。我的数据库有多个像图像的孩子。我想从中获取电话和地址值。我怎么能得到它们?我是Android新手,我在这里尝试了多个答案,但都失败了。谁能帮我这个?
1 个解决方案
#1
0
To get that data, please use the following code:
要获取该数据,请使用以下代码:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("Users");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String address = ds.child("Address").getValue(String.class);
String phone = ds.child("Phone").getValue(String.class);
Log.d("TAG", address + " / " + phone);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(eventListener);
And this is the code to get the address from a single user:
这是从单个用户获取地址的代码:
String uid = firebaseAuth.getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userRef = rootRef.child("Users").child(uid);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String address = dataSnapshot.child("Address").getValue(String.class);
Log.d("TAG", address);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
userRef.addListenerForSingleValueEvent(eventListener);
#1
0
To get that data, please use the following code:
要获取该数据,请使用以下代码:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("Users");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String address = ds.child("Address").getValue(String.class);
String phone = ds.child("Phone").getValue(String.class);
Log.d("TAG", address + " / " + phone);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(eventListener);
And this is the code to get the address from a single user:
这是从单个用户获取地址的代码:
String uid = firebaseAuth.getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userRef = rootRef.child("Users").child(uid);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String address = dataSnapshot.child("Address").getValue(String.class);
Log.d("TAG", address);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
userRef.addListenerForSingleValueEvent(eventListener);