简体   繁体   中英

Else part running even if condition is true in android

I would like to implement a system where when they sign up they provide their username,phone number and a password along with some other data and it all gets saved to the database. So that the user can then signin with their username/phone and password

I am checking if username and password entered is equal to the username and password in the firebase database but it runs the else part even the if part is true, help me to sort out the issue tried with finish() and break statement too.

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
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 MainActivity extends AppCompatActivity {
    ImageView logo;
    public EditText loginPhone, logInpasswd;
    //Button btnLogIn;
    TextView signup,phonetext,btnLogIn;
    DatabaseReference dbview;
    SessionManager sessionManager;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
        //SessionManager session = new SessionManager(MainActivity.this);
        dbview=FirebaseDatabase.getInstance().getReference("Users");
        logo = (ImageView) findViewById(R.id.imageView);
        logo.animate().rotation(360).start();
        loginPhone = findViewById(R.id.loginPhone);
        logInpasswd = findViewById(R.id.loginpaswd);
        btnLogIn = findViewById(R.id.btnLogIn);
        signup = findViewById(R.id.SignIn);
        logo.setImageResource(R.drawable.mgenomics);
        signup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent in = new Intent(MainActivity.this, register.class);
                startActivity(in);
            }
        });
        btnLogIn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
            verifyUser();
            }
        });
    }
    public void verifyUser(){
        sessionManager = new SessionManager(MainActivity.this);

        dbview.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
            for(DataSnapshot data:snapshot.getChildren()) {
                User user = data.getValue(User.class);
                String Phone = loginPhone.getText().toString().trim();
                String Passwd = logInpasswd.getText().toString().trim();
                String uphone = user.getuPhone();
                String upass = user.getuPass();
                String uname = user.getuUname();
                String uid = user.getuID();
                //Bundle data_pass = new Bundle();
                // data_pass.putString("name",uname);

                if(uname!=null && (uname.equals(Phone) || uphone.equals(Phone))  && upass.equals(Passwd)){
                    Intent i = new Intent(MainActivity.this,logged_user.class);
                    sessionManager.saveSession(uname);
                    sessionManager.loginUser(uname,true);
                    startActivity(i);
                    finish();
                    break;
                    }
                else
                {
                    logInpasswd.setError("Invalid Username/Password");
                }

            }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Log.i(TAG, "onCancelled: Error: " + error.getMessage());
            }
        });

    }
}

In if statement if(uname!=null && (uname.equals(Phone) || uphone.equals(Phone)) && upass.equals(Passwd)) this will only works if only username not equal to null it means if you enter uphone number as you described username could be username/phone number this wont work. If we use && operator if the first condition fails it won't check for the rest of the conditions simple it will goto else condition. Please check your values for username.

Rewritten if statement based on your explanation:

 if(((uname!=null && uname.equals(Phone)) || ( uphone !=null|| uphone.equals(Phone)))  && upass.equals(Passwd))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM