简体   繁体   中英

Retrieve child nodes Firebase Database Android into separate variables?

Retrieve child nodes into separate variables from Firebase Database Android.

Screenshot of my database

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);
              }
          }
});

But somehow I cannot retrieve and store the values. Need Help?

Try getKey() instead of child().

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM