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
android firebase firebase-realtime-database firebase-authentication
add a comment |
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
android firebase firebase-realtime-database firebase-authentication
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
add a comment |
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
android firebase firebase-realtime-database firebase-authentication
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
android firebase firebase-realtime-database firebase-authentication
android firebase firebase-realtime-database firebase-authentication
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
add a comment |
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
add a comment |
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());
}
});
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 usingif
. 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 errorjava.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 useString.valueOf(status)
because it's already a String object. Also useequals()
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
|
show 3 more comments
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());
}
});
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 usingif
. 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 errorjava.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 useString.valueOf(status)
because it's already a String object. Also useequals()
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
|
show 3 more comments
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());
}
});
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 usingif
. 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 errorjava.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 useString.valueOf(status)
because it's already a String object. Also useequals()
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
|
show 3 more comments
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());
}
});
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());
}
});
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 usingif
. 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 errorjava.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 useString.valueOf(status)
because it's already a String object. Also useequals()
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
|
show 3 more comments
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 usingif
. 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 errorjava.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 useString.valueOf(status)
because it's already a String object. Also useequals()
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
|
show 3 more comments
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%2f53355996%2fhow-to-check-value-in-realtime-database-on-off%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
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