0

Here is my Firebase database file

package com.example.ram.myapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;



public class LoginActivity extends AppCompatActivity {

  EditText username,password;
  Button login,signUp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    performLoginCheck();
}
void performLoginCheck(){
    final int[] t = new int[1];
    t[0] = 0;
    username= findViewById(R.id.uname);
    password= findViewById(R.id.upass);
    signUp=findViewById(R.id.btnsignup);
    signUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(LoginActivity.this, signUpActivity.class));
        }
    });
    login=findViewById(R.id.btnlogin);
    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final String name=username.getText().toString();
            final String pass=password.getText().toString();

            DatabaseReference database= FirebaseDatabase.getInstance().getReference();

            database.child("profile").addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for(DataSnapshot findKeyValue : dataSnapshot.getChildren()) {
                        if (findKeyValue.child("username").getValue() != null && findKeyValue.child("password").getValue() != null) {
                            String nameFound = (String) findKeyValue.child("username").getValue();
                            String passwordFound = (String) findKeyValue.child("password").getValue();
                            System.out.println(nameFound);
                            System.out.println(passwordFound);
                            System.out.println(name);
                            System.out.println(pass);
                            if (name.equals(nameFound)) {
                                System.out.println("true");
                                if (pass.equals(passwordFound)) {
                                    System.out.println("true go");
                                    t[0] =1;
                                    System.out.println(t[0]);
                                }
                            }
                        }
                    }
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
            if(t[0] == 1) {
                Toast text = Toast.makeText(getApplicationContext(),"Hello,world", Toast.LENGTH_LONG);
                text.show();
            }
            else{
                Toast text = Toast.makeText(getApplicationContext(),"Hello,loser", Toast.LENGTH_LONG);
                text.show();
            }
        }
    });
}

}

The above code is for my login activity and I need to move to the another activity only if the username and password are satisfied. The problem is I need to click the login button twice in order to perform the function. I am new to this firebase database and I need some guidance on onDatachange and How to use it on a button onclick listener.

1 Answers1

0

This is happening because you are setting t[0] = 1 inside the onDataChange() method, which has an asynchronous behaviour which in term means, that by the time you are checking if(t[0] == 1) the data isn't set yet in the database, so this statement will always be evaluated as false for the first time. A quick fix would be to move the declaration of your array int[] t inside the onDataChange() method. Also use all those if statements inside the method. In this way you'll be able to use the right value of t[0]. If you want to use t[0] outsite the method, I recommend you see the last part of my anwser from this post and also take a look at this video.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193