简体   繁体   English

即使在android中条件为真,其他部分也会运行

[英]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.我正在检查输入的用户名和密码是否等于 firebase 数据库中的用户名和密码,但即使 if 部分为真,它也会运行 else 部分,帮助我解决使用 finish() 和 break 语句尝试过的问题。

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 语句中if(uname!=null && (uname.equals(Phone) || uphone.equals(Phone)) && upass.equals(Passwd))这仅在用户名不等于 null 时才有效,这意味着如果您输入您描述的 uphone 号码 username 可能是用户名/电话号码,这行不通。 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 语句:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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