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;
}
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
add a comment |
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
add a comment |
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
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
java
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
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
Hi Turan! Have you tried my solution above, does it work?
– Alex Mamo
Nov 17 '18 at 8:57
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Hi Turan! Have you tried my solution above, does it work?
– Alex Mamo
Nov 17 '18 at 8:57
add a comment |
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.
Hi Turan! Have you tried my solution above, does it work?
– Alex Mamo
Nov 17 '18 at 8:57
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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