将Android Firebase数据库的子节点检索到单独的变量中?

时间:2019-05-06 11:27:46

标签: java android

将子节点从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);
              }
          }
});

但是以某种方式我无法检索和存储值。需要帮助吗?

1 个答案:

答案 0 :(得分:0)

尝试使用getKey()而不是child()。

          if(dataSnapshot.getKey().equals("Continental")){
             Continental = dataSnapshot.getValue(String.class);
             cityTextOne.setText(Continental);
          }