Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

java - Intent not working with if operator Android

I try when it displays a message when I press a button to redirect me to a page, but it doesn't work. When successful, the message is displayed but does not forward me.

private void registerUser() {
    try {

        Account account = new Account();
        account.setpAddress(edt_adress.getText().toString());
        account.setpBloodGroup(edt_blood.getSelectedItem().toString());
        account.setpFirstName(edt_first.getText().toString());
        account.setpMidName(edt_mid.getText().toString());
        account.setpFamilyName(edt_fam.getText().toString());
        account.setpGender(edt_gender.getSelectedItem().toString());
        account.setpPhone(edt_phone.getText().toString());
        account.setpEgn(edt_egn.getText().toString());
        account.setuPassword(edt_password.getText().toString());
        account.setuUsername(edt_username.getText().toString());
        INodeJS accountservice = RetrofitClient.getInstance().create(INodeJS.class);
        Call call = accountservice.create(account);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                Account result = (Account) response.body();
                
                Toast.makeText(getApplicationContext(), result.getNotes(), Toast.LENGTH_LONG).show();
                if (result.getNotes().equals("Success")) {
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                }
                // startActivity(intent);
            }



            @Override
            public void onFailure(Call call, Throwable t) {
                Toast.makeText(getApplicationContext(), getString(R.string.createfield), Toast.LENGTH_SHORT).show();
            }
        });

    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
question from:https://stackoverflow.com/questions/65626422/intent-not-working-with-if-operator-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

looks like result.getNotes().equals("Success") isnt true, are you shure it should be? equals is case-sensitive so "success" doesn't equal "Success" and also " Success " - note whitespaces

maybe you can try with this statement:

result.getNotes().toLowerCase(Locale.getDefault()).trim().equals("success")

toLowerCase will make all your letters small caps and trim will remove all whitespaces at beggining and end of String

also there is equalsIgnoreCase method so it may also looks like below:

result.getNotes().trim().equalsIgnoreCase("success")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...