How to check value in realtime database (ON/OFF)











up vote
0
down vote

favorite












How i can in this code



    @Override
protected void onStart() {
super.onStart();
//if the user is already signed in
//we will close this activity
//and take the user to profile activity
if (mAuth.getCurrentUser() != null) {
finish();
startActivity(new Intent(this, ActivitySplash.class));
}

}


make check whether the child (userId) is set to ON / OFF and if ON then we run the code



    if (mAuth.getCurrentUser() != null) {
finish();
startActivity(new Intent(this, ActivitySplash.class));
}


if OFF then we show a specific activity.



My database
My DB structure










share|improve this question




















  • 1




    You do that by reading the value from the database. Instead of writing the code for you, I recommend you read the documentation on reading values: firebase.google.com/docs/database/android/…
    – Frank van Puffelen
    Nov 17 at 22:34










  • @FrankvanPuffelen, I would love to read, but I'm Russian and English is very weak.On this and requested help with example in order to understand.
    – Nasdomlan Urban3p
    Nov 18 at 6:54















up vote
0
down vote

favorite












How i can in this code



    @Override
protected void onStart() {
super.onStart();
//if the user is already signed in
//we will close this activity
//and take the user to profile activity
if (mAuth.getCurrentUser() != null) {
finish();
startActivity(new Intent(this, ActivitySplash.class));
}

}


make check whether the child (userId) is set to ON / OFF and if ON then we run the code



    if (mAuth.getCurrentUser() != null) {
finish();
startActivity(new Intent(this, ActivitySplash.class));
}


if OFF then we show a specific activity.



My database
My DB structure










share|improve this question




















  • 1




    You do that by reading the value from the database. Instead of writing the code for you, I recommend you read the documentation on reading values: firebase.google.com/docs/database/android/…
    – Frank van Puffelen
    Nov 17 at 22:34










  • @FrankvanPuffelen, I would love to read, but I'm Russian and English is very weak.On this and requested help with example in order to understand.
    – Nasdomlan Urban3p
    Nov 18 at 6:54













up vote
0
down vote

favorite









up vote
0
down vote

favorite











How i can in this code



    @Override
protected void onStart() {
super.onStart();
//if the user is already signed in
//we will close this activity
//and take the user to profile activity
if (mAuth.getCurrentUser() != null) {
finish();
startActivity(new Intent(this, ActivitySplash.class));
}

}


make check whether the child (userId) is set to ON / OFF and if ON then we run the code



    if (mAuth.getCurrentUser() != null) {
finish();
startActivity(new Intent(this, ActivitySplash.class));
}


if OFF then we show a specific activity.



My database
My DB structure










share|improve this question















How i can in this code



    @Override
protected void onStart() {
super.onStart();
//if the user is already signed in
//we will close this activity
//and take the user to profile activity
if (mAuth.getCurrentUser() != null) {
finish();
startActivity(new Intent(this, ActivitySplash.class));
}

}


make check whether the child (userId) is set to ON / OFF and if ON then we run the code



    if (mAuth.getCurrentUser() != null) {
finish();
startActivity(new Intent(this, ActivitySplash.class));
}


if OFF then we show a specific activity.



My database
My DB structure







android firebase firebase-realtime-database firebase-authentication






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 22:33









Frank van Puffelen

220k25361387




220k25361387










asked Nov 17 at 22:08









Nasdomlan Urban3p

136




136








  • 1




    You do that by reading the value from the database. Instead of writing the code for you, I recommend you read the documentation on reading values: firebase.google.com/docs/database/android/…
    – Frank van Puffelen
    Nov 17 at 22:34










  • @FrankvanPuffelen, I would love to read, but I'm Russian and English is very weak.On this and requested help with example in order to understand.
    – Nasdomlan Urban3p
    Nov 18 at 6:54














  • 1




    You do that by reading the value from the database. Instead of writing the code for you, I recommend you read the documentation on reading values: firebase.google.com/docs/database/android/…
    – Frank van Puffelen
    Nov 17 at 22:34










  • @FrankvanPuffelen, I would love to read, but I'm Russian and English is very weak.On this and requested help with example in order to understand.
    – Nasdomlan Urban3p
    Nov 18 at 6:54








1




1




You do that by reading the value from the database. Instead of writing the code for you, I recommend you read the documentation on reading values: firebase.google.com/docs/database/android/…
– Frank van Puffelen
Nov 17 at 22:34




You do that by reading the value from the database. Instead of writing the code for you, I recommend you read the documentation on reading values: firebase.google.com/docs/database/android/…
– Frank van Puffelen
Nov 17 at 22:34












@FrankvanPuffelen, I would love to read, but I'm Russian and English is very weak.On this and requested help with example in order to understand.
– Nasdomlan Urban3p
Nov 18 at 6:54




@FrankvanPuffelen, I would love to read, but I'm Russian and English is very weak.On this and requested help with example in order to understand.
– Nasdomlan Urban3p
Nov 18 at 6:54












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










As @FrankvanPuffelen said, you should spend some time reading docs, it would help you write code yourself, still I am briefing the things for you here. It should make things more clear.



Reading from database is done by correctly referencing your desired node from the database and then using the correct eventListener. There are 3 types of eventListeners present, singleValueEventListener, valueEventListener and childEventListener.



Read more about each of them in detail in docs.
This answer can also help you understand childEventListeners.



To retrieve the value of status node you have to go sequentially through your database parent nodes, that are users and that uid with value nfe....



So in code it would look something like this:



DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(uid);

// uid has the value nfe...


ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String status = dataSnapshot.child("status").getValue(String.class);
// compare the value of status here and do what you want
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, "onCancelled", databaseError.toException());
}


});





share|improve this answer























  • Thank you for the answer, I will try to understand now how to compare the value)
    – Nasdomlan Urban3p
    Nov 18 at 6:55










  • Yes, a bonus but maybe not so useful hint would be using if. Also do mark the answer as correct by clicking on the V type tick mark looking button next to the answer. It should turn green. This helps future readers of the question and I'd appreciate that too. Cheers! :)
    – PradyumanDixit
    Nov 18 at 8:09










  • FirebaseUser user = mAuth.getCurrentUser(); String userId = user.getUid(); DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(userId); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String status = dataSnapshot.child("status").getValue(String.class); if (String.valueOf(status) == String.valueOf("off")) { } } I'm trying to do it, but it doesn't work)
    – Nasdomlan Urban3p
    Nov 18 at 8:34












  • ,i have this error java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference pastebin.com/UZWGVVjf
    – Nasdomlan Urban3p
    Nov 18 at 9:04












  • First thing is that you don't have to use String.valueOf(status) because it's already a String object. Also use equals() method for comparing two strings, so the code would look something like this: if(status.equals("off")) { // do what you want }`
    – PradyumanDixit
    Nov 18 at 9:09













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',
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%2f53355996%2fhow-to-check-value-in-realtime-database-on-off%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








up vote
1
down vote



accepted










As @FrankvanPuffelen said, you should spend some time reading docs, it would help you write code yourself, still I am briefing the things for you here. It should make things more clear.



Reading from database is done by correctly referencing your desired node from the database and then using the correct eventListener. There are 3 types of eventListeners present, singleValueEventListener, valueEventListener and childEventListener.



Read more about each of them in detail in docs.
This answer can also help you understand childEventListeners.



To retrieve the value of status node you have to go sequentially through your database parent nodes, that are users and that uid with value nfe....



So in code it would look something like this:



DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(uid);

// uid has the value nfe...


ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String status = dataSnapshot.child("status").getValue(String.class);
// compare the value of status here and do what you want
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, "onCancelled", databaseError.toException());
}


});





share|improve this answer























  • Thank you for the answer, I will try to understand now how to compare the value)
    – Nasdomlan Urban3p
    Nov 18 at 6:55










  • Yes, a bonus but maybe not so useful hint would be using if. Also do mark the answer as correct by clicking on the V type tick mark looking button next to the answer. It should turn green. This helps future readers of the question and I'd appreciate that too. Cheers! :)
    – PradyumanDixit
    Nov 18 at 8:09










  • FirebaseUser user = mAuth.getCurrentUser(); String userId = user.getUid(); DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(userId); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String status = dataSnapshot.child("status").getValue(String.class); if (String.valueOf(status) == String.valueOf("off")) { } } I'm trying to do it, but it doesn't work)
    – Nasdomlan Urban3p
    Nov 18 at 8:34












  • ,i have this error java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference pastebin.com/UZWGVVjf
    – Nasdomlan Urban3p
    Nov 18 at 9:04












  • First thing is that you don't have to use String.valueOf(status) because it's already a String object. Also use equals() method for comparing two strings, so the code would look something like this: if(status.equals("off")) { // do what you want }`
    – PradyumanDixit
    Nov 18 at 9:09

















up vote
1
down vote



accepted










As @FrankvanPuffelen said, you should spend some time reading docs, it would help you write code yourself, still I am briefing the things for you here. It should make things more clear.



Reading from database is done by correctly referencing your desired node from the database and then using the correct eventListener. There are 3 types of eventListeners present, singleValueEventListener, valueEventListener and childEventListener.



Read more about each of them in detail in docs.
This answer can also help you understand childEventListeners.



To retrieve the value of status node you have to go sequentially through your database parent nodes, that are users and that uid with value nfe....



So in code it would look something like this:



DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(uid);

// uid has the value nfe...


ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String status = dataSnapshot.child("status").getValue(String.class);
// compare the value of status here and do what you want
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, "onCancelled", databaseError.toException());
}


});





share|improve this answer























  • Thank you for the answer, I will try to understand now how to compare the value)
    – Nasdomlan Urban3p
    Nov 18 at 6:55










  • Yes, a bonus but maybe not so useful hint would be using if. Also do mark the answer as correct by clicking on the V type tick mark looking button next to the answer. It should turn green. This helps future readers of the question and I'd appreciate that too. Cheers! :)
    – PradyumanDixit
    Nov 18 at 8:09










  • FirebaseUser user = mAuth.getCurrentUser(); String userId = user.getUid(); DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(userId); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String status = dataSnapshot.child("status").getValue(String.class); if (String.valueOf(status) == String.valueOf("off")) { } } I'm trying to do it, but it doesn't work)
    – Nasdomlan Urban3p
    Nov 18 at 8:34












  • ,i have this error java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference pastebin.com/UZWGVVjf
    – Nasdomlan Urban3p
    Nov 18 at 9:04












  • First thing is that you don't have to use String.valueOf(status) because it's already a String object. Also use equals() method for comparing two strings, so the code would look something like this: if(status.equals("off")) { // do what you want }`
    – PradyumanDixit
    Nov 18 at 9:09















up vote
1
down vote



accepted







up vote
1
down vote



accepted






As @FrankvanPuffelen said, you should spend some time reading docs, it would help you write code yourself, still I am briefing the things for you here. It should make things more clear.



Reading from database is done by correctly referencing your desired node from the database and then using the correct eventListener. There are 3 types of eventListeners present, singleValueEventListener, valueEventListener and childEventListener.



Read more about each of them in detail in docs.
This answer can also help you understand childEventListeners.



To retrieve the value of status node you have to go sequentially through your database parent nodes, that are users and that uid with value nfe....



So in code it would look something like this:



DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(uid);

// uid has the value nfe...


ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String status = dataSnapshot.child("status").getValue(String.class);
// compare the value of status here and do what you want
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, "onCancelled", databaseError.toException());
}


});





share|improve this answer














As @FrankvanPuffelen said, you should spend some time reading docs, it would help you write code yourself, still I am briefing the things for you here. It should make things more clear.



Reading from database is done by correctly referencing your desired node from the database and then using the correct eventListener. There are 3 types of eventListeners present, singleValueEventListener, valueEventListener and childEventListener.



Read more about each of them in detail in docs.
This answer can also help you understand childEventListeners.



To retrieve the value of status node you have to go sequentially through your database parent nodes, that are users and that uid with value nfe....



So in code it would look something like this:



DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(uid);

// uid has the value nfe...


ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String status = dataSnapshot.child("status").getValue(String.class);
// compare the value of status here and do what you want
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, "onCancelled", databaseError.toException());
}


});






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 18 at 6:06

























answered Nov 18 at 3:40









PradyumanDixit

2,1471718




2,1471718












  • Thank you for the answer, I will try to understand now how to compare the value)
    – Nasdomlan Urban3p
    Nov 18 at 6:55










  • Yes, a bonus but maybe not so useful hint would be using if. Also do mark the answer as correct by clicking on the V type tick mark looking button next to the answer. It should turn green. This helps future readers of the question and I'd appreciate that too. Cheers! :)
    – PradyumanDixit
    Nov 18 at 8:09










  • FirebaseUser user = mAuth.getCurrentUser(); String userId = user.getUid(); DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(userId); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String status = dataSnapshot.child("status").getValue(String.class); if (String.valueOf(status) == String.valueOf("off")) { } } I'm trying to do it, but it doesn't work)
    – Nasdomlan Urban3p
    Nov 18 at 8:34












  • ,i have this error java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference pastebin.com/UZWGVVjf
    – Nasdomlan Urban3p
    Nov 18 at 9:04












  • First thing is that you don't have to use String.valueOf(status) because it's already a String object. Also use equals() method for comparing two strings, so the code would look something like this: if(status.equals("off")) { // do what you want }`
    – PradyumanDixit
    Nov 18 at 9:09




















  • Thank you for the answer, I will try to understand now how to compare the value)
    – Nasdomlan Urban3p
    Nov 18 at 6:55










  • Yes, a bonus but maybe not so useful hint would be using if. Also do mark the answer as correct by clicking on the V type tick mark looking button next to the answer. It should turn green. This helps future readers of the question and I'd appreciate that too. Cheers! :)
    – PradyumanDixit
    Nov 18 at 8:09










  • FirebaseUser user = mAuth.getCurrentUser(); String userId = user.getUid(); DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(userId); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String status = dataSnapshot.child("status").getValue(String.class); if (String.valueOf(status) == String.valueOf("off")) { } } I'm trying to do it, but it doesn't work)
    – Nasdomlan Urban3p
    Nov 18 at 8:34












  • ,i have this error java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference pastebin.com/UZWGVVjf
    – Nasdomlan Urban3p
    Nov 18 at 9:04












  • First thing is that you don't have to use String.valueOf(status) because it's already a String object. Also use equals() method for comparing two strings, so the code would look something like this: if(status.equals("off")) { // do what you want }`
    – PradyumanDixit
    Nov 18 at 9:09


















Thank you for the answer, I will try to understand now how to compare the value)
– Nasdomlan Urban3p
Nov 18 at 6:55




Thank you for the answer, I will try to understand now how to compare the value)
– Nasdomlan Urban3p
Nov 18 at 6:55












Yes, a bonus but maybe not so useful hint would be using if. Also do mark the answer as correct by clicking on the V type tick mark looking button next to the answer. It should turn green. This helps future readers of the question and I'd appreciate that too. Cheers! :)
– PradyumanDixit
Nov 18 at 8:09




Yes, a bonus but maybe not so useful hint would be using if. Also do mark the answer as correct by clicking on the V type tick mark looking button next to the answer. It should turn green. This helps future readers of the question and I'd appreciate that too. Cheers! :)
– PradyumanDixit
Nov 18 at 8:09












FirebaseUser user = mAuth.getCurrentUser(); String userId = user.getUid(); DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(userId); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String status = dataSnapshot.child("status").getValue(String.class); if (String.valueOf(status) == String.valueOf("off")) { } } I'm trying to do it, but it doesn't work)
– Nasdomlan Urban3p
Nov 18 at 8:34






FirebaseUser user = mAuth.getCurrentUser(); String userId = user.getUid(); DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(userId); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String status = dataSnapshot.child("status").getValue(String.class); if (String.valueOf(status) == String.valueOf("off")) { } } I'm trying to do it, but it doesn't work)
– Nasdomlan Urban3p
Nov 18 at 8:34














,i have this error java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference pastebin.com/UZWGVVjf
– Nasdomlan Urban3p
Nov 18 at 9:04






,i have this error java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference pastebin.com/UZWGVVjf
– Nasdomlan Urban3p
Nov 18 at 9:04














First thing is that you don't have to use String.valueOf(status) because it's already a String object. Also use equals() method for comparing two strings, so the code would look something like this: if(status.equals("off")) { // do what you want }`
– PradyumanDixit
Nov 18 at 9:09






First thing is that you don't have to use String.valueOf(status) because it's already a String object. Also use equals() method for comparing two strings, so the code would look something like this: if(status.equals("off")) { // do what you want }`
– PradyumanDixit
Nov 18 at 9:09




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53355996%2fhow-to-check-value-in-realtime-database-on-off%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

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]