将子节点从Firebase数据库Android中检索到单独的变量中。
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myNewRef = database.getReference("F-5/Islamabad Marriott Hotel");
myNewRef.addChildEventListener(new ChildEventListener() {
public String FastFood;
public String Desi;
public String Continental;
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
if(dataSnapshot.child("Continental")!= null){
Continental = dataSnapshot.getValue(String.class);
cityTextOne.setText(Continental);
}
if(dataSnapshot.child("Desi")!= null){
Desi = dataSnapshot.getValue(String.class);
cityTextTwo.setText(Desi);
}
if(dataSnapshot.child("Fast-Food")!= null){
FastFood = dataSnapshot.getValue(String.class);
cityTextThree.setText(FastFood);
}
}
});
但是以某种方式我无法检索和存储值。需要帮助吗?
答案 0 :(得分:0)
尝试使用getKey()而不是child()。
if(dataSnapshot.getKey().equals("Continental")){
Continental = dataSnapshot.getValue(String.class);
cityTextOne.setText(Continental);
}