Я столкнулся с проблемой при переходе к основному действию из активности входа, сравнивая параметры действий входа и регистрации - PullRequest
0 голосов
/ 15 сентября 2018

Я создал формы регистрации и входа. Мои данные будут храниться в SharedPreferences. Когда я вписываю адрес электронной почты и пароль в форму входа в систему, она должна сравниваться с данными, которые я предоставляю в форме регистрации, и переходить к другой Activity. Но когда я ввожу данные, она не работает, как я ожидал, и отображает сообщение, которое я Toast. Ниже приведены файлы регистрации и входа.

Регистрационный код:

 public class SignUp extends AppCompatActivity {
EditText name,email,password,mobile,securityAnswer;
Spinner spinner;
Button signup,clear;
public static SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);

    //Getting id's

    name = findViewById(R.id.name);
    email = findViewById(R.id.email);
    password = findViewById(R.id.pass);
    mobile=findViewById(R.id.number);
    securityAnswer=findViewById(R.id.spanswer);
    signup=findViewById(R.id.signup);
    clear=findViewById(R.id.clear);
    spinner=findViewById(R.id.spinner);

    //Creating file called UserDetails

    sp=getSharedPreferences("UserDetails",MODE_PRIVATE);

    //Questions to be inserted in spinner

    String[] questions={"Select security question","Who is your favourite actor","What is your first school name","Who is the founder of android","What was your first mobile number","What was your favourite childhood cartoon program"};
    ArrayAdapter adapter=new ArrayAdapter(getApplicationContext(),android.R.layout.simple_spinner_item,questions);
    spinner.setAdapter(adapter);

    //Setting Listener to submit button

    signup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            //Getting the data from user

            String userName=name.getText().toString();
            String userEmail=email.getText().toString();
            String userPass=password.getText().toString();
            String userMobile=mobile.getText().toString();
            String userQuestion=spinner.getSelectedItem().toString();
            String userAnswer=securityAnswer.getText().toString();

            //Inserting the data into SharedPreference

            SharedPreferences.Editor editor=sp.edit();
            editor.putString("NAME",userName);
            editor.putString("EMAIL",userEmail);
            editor.putString("PASSWORD",userPass);
            editor.putString("MOBILE",userMobile);
            editor.putString("QUESTION",userQuestion);
            editor.putString("ANSWER",userAnswer);
            editor.commit();
            Toast.makeText(SignUp.this, "Data Submitted Successfully", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(getApplicationContext(),Login.class));
        }
    });

    //Setting Listener to clear button

    clear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            name.setText("");
            email.setText("");
            password.setText("");
            mobile.setText("");
            spinner.setSelection(0);
            securityAnswer.setText("");
        }
    });
}
}

Код входа:

public class Login extends AppCompatActivity {

//Declaring widgets

 EditText email,password;
 Button login,clear;
 TextView forgot;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    //Finding id's for each widget
    email=findViewById(R.id.loginemail);
    password=findViewById(R.id.loginpassword);
    login=findViewById(R.id.login);
    clear=findViewById(R.id.clearlogin);
    forgot=findViewById(R.id.forgotpassword);

    //Setting Listener to Login Button

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            compareData();
        }
    });

    //Setting Listener to clear button

    clear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            email.setText("");
            password.setText("");
        }
    });

    //Setting Listener to forgot password TextView

    forgot.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(getApplicationContext(),ForgotPassword.class));
        }
    });
}
public void compareData(){
    //Getting Text from user and converting to String

    String login_email=email.getText().toString();
    String login_password=password.getText().toString();

    //Getting the stored data from sharedPreferences given by user while sign up

    String sp_email=SignUp.sp.getString("EMAIL","Email is not available");
    String sp_pass=SignUp.sp.getString("PASSWORD","Password is incorrect");

    //Comparing email & password inserted by user while sign up and login
    //if same,navigate to home screen.If not,Display the message.

    if(login_email==sp_email){
        if(login_password==sp_pass){
            startActivity(new Intent(getApplicationContext(),HomeScreen.class));
        }
    }else{
        Toast.makeText(Login.this, "Please Enter Correct details", Toast.LENGTH_SHORT).show();
    }
}
}

1 Ответ

0 голосов
/ 15 сентября 2018

вы должны использовать String.equals(), а не ==

Так что измените это

if(login_email==sp_email){
    if(login_password==sp_pass){
        startActivity(new Intent(getApplicationContext(),HomeScreen.class));
    }
}

на

if(login_email.equals(sp_email)){
    if(login_password.equals(sp_pass)){
        startActivity(new Intent(getApplicationContext(),HomeScreen.class));
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...