title: Android Firebase how to loop through EVERY key of the list
body: <p>In the Firebase realtime database, I have created a list with unique key for every list items.</p>
  
  <pre><code>String deviceId = dbDevice.push().getKey();
  dbDevice.child(deviceId).setValue(device);
  </code></pre>
  
  <p>While in the other Activity, I would like to loop through each device by key, and call the method updateLatestDistance for each particular device.</p>
  
  <p>Though the results of the code below functions in a weird way.
  E.g. I have 2 list item, so I'll have two distance attribute, one for each list item. While the updateLatestDistance method is called, the distance value in the particular listitem should be updated, though it results in both listItem will contains both 2 latestDistance.</p>
  
  <pre><code>Database dbDev= FirebaseDatabase.getInstance().getReference("Device");
  dbDev.addValueEventListener(new ValueEventListener() {
  
      //In the onstart method
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) { //Read the value
              //Clear and regenerate the list so that no item is multipled.
              devices.clear();
              //Loop
              for(DataSnapshot deviceSnapshot : dataSnapshot.getChildren()){
  
                  Device device = deviceSnapshot.getValue(Device.class);
                  //Add the fetched data into the local list.
                  devices.add(device);
  
                  String checkId = deviceSnapshot.getKey();
  
                  for(int i =0; i&lt;devices.size(); i++){
                      //Grab the latest safety_zone's latitude for later usage.
                      DatabaseReference dbSafe_La = FirebaseDatabase.getInstance().getReference("Device/" +checkId+ "/SafetyZone/s_latitude");
                      dbSafe_La.addListenerForSingleValueEvent(new ValueEventListener() {
                          @Override
                          public void onDataChange(DataSnapshot dataSnapshot) {
                              //Check if safetyZone exists.
                              //If no, a 0.0 is passed.
                              if(dataSnapshot.exists()){
                                  la = dataSnapshot.getValue(double.class);
                              }
                          }
  
                          @Override
                          public void onCancelled(DatabaseError databaseError) {
                          }
                      });
                      //Grab the latest safety_zone's longtitude for later usage.
                      DatabaseReference dbSafe_Lo = FirebaseDatabase.getInstance().getReference("Device/" +checkId+ "/SafetyZone/s_longitude");
                      dbSafe_Lo.addListenerForSingleValueEvent(new ValueEventListener() {
                          @Override
                          public void onDataChange(DataSnapshot dataSnapshot) {
                              //Check if safetyZone exists.
                              //If no, a 0.0 is passed.
                              if(dataSnapshot.exists()){
                                  lo = dataSnapshot.getValue(double.class);
                              }
                          }
                          @Override
                          public void onCancelled(DatabaseError databaseError) {
  
                          }
                      });
  
                  }
                  /////////////////////////////////////////////////////
  
  
                  updateLatestDistance(checkId, la, lo);
  
                  ///showUpdateDidalog(device.getDeviceId(), device.getNickname());
              }
  
              //Create a new adapter
              DeviceListAdapter a = new DeviceListAdapter(Activity_Main.this, devices);
              listViewDevices.setAdapter(a);
          }
  
          @Override
          public void onCancelled(DatabaseError databaseError) {
  
          }
      });
  </code></pre>
  
  <p>Here is the screenshot from the Firebase console as requested. </p>
  
  <p><a href="https://i.stack.imgur.com/KQLpm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KQLpm.png" alt="enter image description here"></a></p>
  
  <p>Here is the updateLatestDistance method as requested.</p>
  
  <pre><code>//Function - Update the distance
      private boolean updateLatestDistance(String deviceId, double safety_la, double safety_lo){
  
          //Compute the latest distance between device's current location &amp; selected safetyzone location.
          //To provide real-time distance changing
          //Firebase - Push latest distance.
          DatabaseReference dbLatest_distance = FirebaseDatabase.getInstance().getReference("Device/" +deviceId+ "/distance");
          DatabaseReference dbLatest_extra = FirebaseDatabase.getInstance().getReference("Device/" +deviceId+ "/extra");
  
          //Check if safetyZone defined
          if(safety_la!=0.0){
              float[] new_d = new float[3];
              //Compute the distance AGAIN.*
              Location.distanceBetween( Double.parseDouble(latitude), Double.parseDouble(longtitude), safety_la, safety_lo, new_d);
  
              //Push the distance data to the Firebase AGAIN.*
              dbLatest_distance.setValue(new_d[0]+"m");
  
              //If distance &lt; 50m, trigger the alarm on the device (Raspberrypi)
              //By Sending 'on' or 'off' to Firebase.
              if(new_d[0] &lt; 50){
                  dbLatest_extra.setValue("on");
              }else{
                  dbLatest_extra.setValue("off");
              }
          }
  
          return true;
      }
  </code></pre>