title: Retrieve complex nested data from firebase
body: <p>I have the data in firebase as given below:
  <a href="https://i.stack.imgur.com/jkcai.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jkcai.png" alt="Firebase screenshot"></a></p>
  
  <p>I want to get the Customer and Device details for each of the Form ID. The Form IDs are generated uniquely by firebase.
  I just somehow need to get access to these Form IDs.</p>
  
  <p>I have set the databaseReference as follows:</p>
  
  <pre><code>databaseReference = FirebaseDatabase.getInstance().getReference("users").child(userID).child("forms");
  </code></pre>
  
  <p>Then I have tried the following code to retrieve the required data:</p>
  
  <pre><code>databaseReference.addValueEventListener(new ValueEventListener() {
              @Override
              public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                  for (DataSnapshot formsSnapshot : dataSnapshot.getChildren()) {
                      for (DataSnapshot formIDSnapshot : formsSnapshot.getChildren()) {
  
                          Device device = formIDSnapshot.getValue(Device.class);
                          if (device != null) {
                              // get and use the data
                          }
                      }
                  }
              }
  
              @Override
              public void onCancelled(@NonNull DatabaseError databaseError) {
  
              }
          });
  </code></pre>
  
  <p><strong>Update:</strong>
  I am now successfully able to retrieve the form IDs, thanks to the answer by @Frank.
  Now, to get the Device and Customer details I have to write the following code inside the ValueEventListener after getting the form IDs:</p>
  
  <pre><code>databaseReference.addValueEventListener(new ValueEventListener() {
              @Override
              public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                  for (DataSnapshot formsSnapshot : dataSnapshot.getChildren()) {
                      String formID = formsSnapshot.getKey();    //Retrieving the formID here
  
              //New code addition
              // to get the reference to device details node
                      databaseReference.child(Objects.requireNonNull(formID)).child("Device details").addValueEventListener(new ValueEventListener() {
                          @Override
                          public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                              Device device = dataSnapshot.getValue(Device.class);
                              if (device != null) {
                                  // required device details here
                              }
                          }
  
                          @Override
                          public void onCancelled(@NonNull DatabaseError databaseError) {
                              throw databaseError.toException();
                          }
                      });
  
              // to get the reference to customer details node
                databaseReference.child(Objects.requireNonNull(formID)).child("Customer details").addValueEventListener(new ValueEventListener() {
                          @Override
                          public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                              Customer customer = dataSnapshot.getValue(Customer.class);
                              if (customer != null) {
                                  //required customer details here
                              }
                          }
  
                          @Override
                          public void onCancelled(@NonNull DatabaseError databaseError) {
                              throw databaseError.toException();
                          }
                      });
                  }
              }
  
              @Override
              public void onCancelled(@NonNull DatabaseError databaseError) {
                  throw databaseError.toException();
              }
          });
  </code></pre>
  
  <p>I understand that this is a messy code and I would like to get some cleaner approach for the same.</p>