How to get two variables from Firebase realtime database





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















static double lat2;

static double lon2;

private void InitMapElements() {

DatabaseReference zonesRef = FirebaseDatabase.getInstance().getReference("markers");

DatabaseReference zone1Ref = zonesRef.child("m-1"); //database path
DatabaseReference zone1NameRef = zone1Ref.child("latit");

DatabaseReference zone2NameRef = zone1Ref.child("longit");

zone1NameRef.addValueEventListener(new ValueEventListener() { //reading the first coordinate

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Double lat1 = dataSnapshot.getValue(Double.class);
lat2 = lat1;
}

@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
}
});
zone2NameRef.addValueEventListener(new ValueEventListener() { //reading the second coordinate
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Double lon1 = dataSnapshot.getValue(Double.class);
lon2 = lon1;

}

@Override
public void onCancelled(DatabaseError error) {
}
});
}


It is not possible to get two variables online and then save it to a static variable from the Firebase realtime database, the variables get the value only once when the method is called. With one variable, everything works fine. The variable changes if you change the value on the base, but with two does not work.



example of working with one variable










share|improve this question































    0















    static double lat2;

    static double lon2;

    private void InitMapElements() {

    DatabaseReference zonesRef = FirebaseDatabase.getInstance().getReference("markers");

    DatabaseReference zone1Ref = zonesRef.child("m-1"); //database path
    DatabaseReference zone1NameRef = zone1Ref.child("latit");

    DatabaseReference zone2NameRef = zone1Ref.child("longit");

    zone1NameRef.addValueEventListener(new ValueEventListener() { //reading the first coordinate

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
    Double lat1 = dataSnapshot.getValue(Double.class);
    lat2 = lat1;
    }

    @Override
    public void onCancelled(DatabaseError error) {
    // Failed to read value
    }
    });
    zone2NameRef.addValueEventListener(new ValueEventListener() { //reading the second coordinate
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
    Double lon1 = dataSnapshot.getValue(Double.class);
    lon2 = lon1;

    }

    @Override
    public void onCancelled(DatabaseError error) {
    }
    });
    }


    It is not possible to get two variables online and then save it to a static variable from the Firebase realtime database, the variables get the value only once when the method is called. With one variable, everything works fine. The variable changes if you change the value on the base, but with two does not work.



    example of working with one variable










    share|improve this question



























      0












      0








      0








      static double lat2;

      static double lon2;

      private void InitMapElements() {

      DatabaseReference zonesRef = FirebaseDatabase.getInstance().getReference("markers");

      DatabaseReference zone1Ref = zonesRef.child("m-1"); //database path
      DatabaseReference zone1NameRef = zone1Ref.child("latit");

      DatabaseReference zone2NameRef = zone1Ref.child("longit");

      zone1NameRef.addValueEventListener(new ValueEventListener() { //reading the first coordinate

      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
      Double lat1 = dataSnapshot.getValue(Double.class);
      lat2 = lat1;
      }

      @Override
      public void onCancelled(DatabaseError error) {
      // Failed to read value
      }
      });
      zone2NameRef.addValueEventListener(new ValueEventListener() { //reading the second coordinate
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
      Double lon1 = dataSnapshot.getValue(Double.class);
      lon2 = lon1;

      }

      @Override
      public void onCancelled(DatabaseError error) {
      }
      });
      }


      It is not possible to get two variables online and then save it to a static variable from the Firebase realtime database, the variables get the value only once when the method is called. With one variable, everything works fine. The variable changes if you change the value on the base, but with two does not work.



      example of working with one variable










      share|improve this question
















      static double lat2;

      static double lon2;

      private void InitMapElements() {

      DatabaseReference zonesRef = FirebaseDatabase.getInstance().getReference("markers");

      DatabaseReference zone1Ref = zonesRef.child("m-1"); //database path
      DatabaseReference zone1NameRef = zone1Ref.child("latit");

      DatabaseReference zone2NameRef = zone1Ref.child("longit");

      zone1NameRef.addValueEventListener(new ValueEventListener() { //reading the first coordinate

      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
      Double lat1 = dataSnapshot.getValue(Double.class);
      lat2 = lat1;
      }

      @Override
      public void onCancelled(DatabaseError error) {
      // Failed to read value
      }
      });
      zone2NameRef.addValueEventListener(new ValueEventListener() { //reading the second coordinate
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
      Double lon1 = dataSnapshot.getValue(Double.class);
      lon2 = lon1;

      }

      @Override
      public void onCancelled(DatabaseError error) {
      }
      });
      }


      It is not possible to get two variables online and then save it to a static variable from the Firebase realtime database, the variables get the value only once when the method is called. With one variable, everything works fine. The variable changes if you change the value on the base, but with two does not work.



      example of working with one variable







      java android firebase firebase-realtime-database






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 12:28









      Alex Mamo

      47.2k82965




      47.2k82965










      asked Nov 16 '18 at 11:06









      Turan BusTuran Bus

      1




      1
























          1 Answer
          1






          active

          oldest

          votes


















          1














          There is no need to attach two listeners in order to get those two values. A single listener can solve your problem. To solve this, please use the following lines of code:



          DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
          DatabaseReference ref = rootRef.child("markers").child("m-1");
          ValueEventListener valueEventListener = new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
          double lat = dataSnapshot.child("latit").getValue(Double.class);
          double lon = dataSnapshot.child("longit").getValue(Double.class);
          Log.d(TAG, lat + ", " + lon);
          }

          @Override
          public void onCancelled(@NonNull DatabaseError databaseError) {
          Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
          }
          };
          ref.addListenerForSingleValueEvent(valueEventListener);


          As you can see, both values are extracted from the DataSnapshot object. Both variables lat and lon will be only be availabe inside the callback, inside onDataChange() due the asynchronous behavior of this method. If you want to use them outiside, I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.






          share|improve this answer
























          • Hi Turan! Have you tried my solution above, does it work?

            – Alex Mamo
            Nov 17 '18 at 8:57












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336613%2fhow-to-get-two-variables-from-firebase-realtime-database%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          There is no need to attach two listeners in order to get those two values. A single listener can solve your problem. To solve this, please use the following lines of code:



          DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
          DatabaseReference ref = rootRef.child("markers").child("m-1");
          ValueEventListener valueEventListener = new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
          double lat = dataSnapshot.child("latit").getValue(Double.class);
          double lon = dataSnapshot.child("longit").getValue(Double.class);
          Log.d(TAG, lat + ", " + lon);
          }

          @Override
          public void onCancelled(@NonNull DatabaseError databaseError) {
          Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
          }
          };
          ref.addListenerForSingleValueEvent(valueEventListener);


          As you can see, both values are extracted from the DataSnapshot object. Both variables lat and lon will be only be availabe inside the callback, inside onDataChange() due the asynchronous behavior of this method. If you want to use them outiside, I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.






          share|improve this answer
























          • Hi Turan! Have you tried my solution above, does it work?

            – Alex Mamo
            Nov 17 '18 at 8:57
















          1














          There is no need to attach two listeners in order to get those two values. A single listener can solve your problem. To solve this, please use the following lines of code:



          DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
          DatabaseReference ref = rootRef.child("markers").child("m-1");
          ValueEventListener valueEventListener = new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
          double lat = dataSnapshot.child("latit").getValue(Double.class);
          double lon = dataSnapshot.child("longit").getValue(Double.class);
          Log.d(TAG, lat + ", " + lon);
          }

          @Override
          public void onCancelled(@NonNull DatabaseError databaseError) {
          Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
          }
          };
          ref.addListenerForSingleValueEvent(valueEventListener);


          As you can see, both values are extracted from the DataSnapshot object. Both variables lat and lon will be only be availabe inside the callback, inside onDataChange() due the asynchronous behavior of this method. If you want to use them outiside, I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.






          share|improve this answer
























          • Hi Turan! Have you tried my solution above, does it work?

            – Alex Mamo
            Nov 17 '18 at 8:57














          1












          1








          1







          There is no need to attach two listeners in order to get those two values. A single listener can solve your problem. To solve this, please use the following lines of code:



          DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
          DatabaseReference ref = rootRef.child("markers").child("m-1");
          ValueEventListener valueEventListener = new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
          double lat = dataSnapshot.child("latit").getValue(Double.class);
          double lon = dataSnapshot.child("longit").getValue(Double.class);
          Log.d(TAG, lat + ", " + lon);
          }

          @Override
          public void onCancelled(@NonNull DatabaseError databaseError) {
          Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
          }
          };
          ref.addListenerForSingleValueEvent(valueEventListener);


          As you can see, both values are extracted from the DataSnapshot object. Both variables lat and lon will be only be availabe inside the callback, inside onDataChange() due the asynchronous behavior of this method. If you want to use them outiside, I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.






          share|improve this answer













          There is no need to attach two listeners in order to get those two values. A single listener can solve your problem. To solve this, please use the following lines of code:



          DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
          DatabaseReference ref = rootRef.child("markers").child("m-1");
          ValueEventListener valueEventListener = new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
          double lat = dataSnapshot.child("latit").getValue(Double.class);
          double lon = dataSnapshot.child("longit").getValue(Double.class);
          Log.d(TAG, lat + ", " + lon);
          }

          @Override
          public void onCancelled(@NonNull DatabaseError databaseError) {
          Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
          }
          };
          ref.addListenerForSingleValueEvent(valueEventListener);


          As you can see, both values are extracted from the DataSnapshot object. Both variables lat and lon will be only be availabe inside the callback, inside onDataChange() due the asynchronous behavior of this method. If you want to use them outiside, I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 12:28









          Alex MamoAlex Mamo

          47.2k82965




          47.2k82965













          • Hi Turan! Have you tried my solution above, does it work?

            – Alex Mamo
            Nov 17 '18 at 8:57



















          • Hi Turan! Have you tried my solution above, does it work?

            – Alex Mamo
            Nov 17 '18 at 8:57

















          Hi Turan! Have you tried my solution above, does it work?

          – Alex Mamo
          Nov 17 '18 at 8:57





          Hi Turan! Have you tried my solution above, does it work?

          – Alex Mamo
          Nov 17 '18 at 8:57




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336613%2fhow-to-get-two-variables-from-firebase-realtime-database%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          The Sandy Post

          Danny Elfman

          Pages that link to "Head v. Amoskeag Manufacturing Co."