0

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 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: https://firebase.google.com/docs/database/android/read-and-write#listen_for_value_events – Frank van Puffelen Nov 17 '18 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 '18 at 06:54

1 Answers1

0

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());
            }


        });
PradyumanDixit
  • 2,372
  • 2
  • 12
  • 20
  • Thank you for the answer, I will try to understand now how to compare the value) – Nasdomlan Urban3p Nov 18 '18 at 06: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 '18 at 08: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 '18 at 08: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` https://pastebin.com/UZWGVVjf – Nasdomlan Urban3p Nov 18 '18 at 09: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 '18 at 09:09
  • `java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference` **My code** `if(status.equals("off")) { Toast.makeText(ActivitySplash.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); }else { //code }` – Nasdomlan Urban3p Nov 18 '18 at 09:14
  • Have you placed correct `uid` in the code `.child(uid)`. Check that you are getting the value properly, using `Log.d("TAG", status)`. – PradyumanDixit Nov 18 '18 at 09:18
  • `java.lang.NullPointerException: println needs a message` `at app.thecity.ActivitySplash$1.onDataChange(ActivitySplash.java:76)` in `Log.d("TAG", status);` – Nasdomlan Urban3p Nov 18 '18 at 10:09