英文:
How to retrieve the child data from Firebase?
问题
以下是翻译好的部分:
private void loadAddress() {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
ref.orderByChild("uid").equalTo(firebaseAuth.getUid())
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
String name = "" + ds.child("name").getValue();
String lat = "" + ds.child("name2").getValue();
addresstv.setText(name);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
英文:
My database looks as follows, in here I want to retrieve the data inside the mylocation
child (see the attached image here). But it is not working.
How to solve that?
My current code looks as this,
private void loadAddress() {
DatabaseReference ref= FirebaseDatabase.getInstance().getReference("Users");
ref.orderByChild("uid").equalTo(firebaseAuth.getUid())
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()){
String name=""+ds.child("name").getValue();
String lat=""+ds.child("name2").getValue();
addresstv.setText(name);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
答案1
得分: 1
获取mylocation
下的数据,您需要显式调用该子项,就像以下代码中所示:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("mylocation").child("name2").getValue(String.class);
double latitude = dataSnapshot.child("mylocation").child("latitude").getValue(Double.class);
double longitude = dataSnapshot.child("mylocation").child("longitude").getValue(Double.class);
Log.d("TAG", name + "/" + latitude + "/" + longitude);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); // 不要忽略错误!
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
日志中的结果将是:
5555/79.8571577/6.9448882
英文:
To get the data under mylocation
, you use an explicit call to that child, like in the following lines of code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("mylocation").child("name2").getValue(String.class);
double latitude = dataSnapshot.child("mylocation").child("latitude").getValue(Double.class);
double longitude = dataSnapshot.child("mylocation").child("longitude").getValue(Double.class);
Log.d("TAG", name + "/" + latitude + "/" + longitude);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
The result in the logcat will be:
5555/79.8571577/6.9448882
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论