Search code examples
if-statementandroid-imageview

set Image Resource and if-else issue


I am trying to change image shown in ImageView according to the String in TextView (excuse me for the nested if-else statements). In the tests that I have run, even though the value of st1 is "HKD", the flagName that get pass out from the nested if-else is still "@drawable/foreign_exchange".

I cannot seem to find the issue. Any help will be much appreciated. Thanks.

    EditText etCurrency = findViewById(R.id.editTextCurrency);
    String st1 = etCurrency.getText().toString();

    if (st1=="HKD"){
        flagName = "@drawable/flag_hong_kong";
    }else if(st1=="IDR"){
        flagName = "@drawable/flag_indonesia";
    }else if(st1=="JPY"){
        flagName = "@drawable/flag_japan";
    }else if(st1=="KRW"){
        flagName = "@drawable/flag_south_korea";
    }else if(st1=="MYR"){
        flagName = "@drawable/flag_malaysia";
    }else if(st1=="NZD"){
        flagName = "@drawable/flag_new_zealand";
    }else if(st1=="PHP"){
        flagName = "@drawable/flag_philippines";
    }else if(st1=="THB"){
        flagName = "@drawable/flag_thailand";
    }else if(st1=="TWD"){
        flagName = "@drawable/flag_taiwan";
    }else flagName = "@drawable/foreign_exchange";

    int imageResource = getResources().getIdentifier(flagName,null,this.getPackageName());
    imageView.setImageResource(imageResource);

Solution

  • use .equals instead of ==

    if (st1.equals("HKD"))