String (eg. "C"), I got from PreferenceManager, does not equals to "C"

android

32 просмотра

1 ответ

I am facing weird situation here.

Following routine is called after I close my preference activity.

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String tmp = preferences.getString("unit", "C");
CommonTool.temperatureUnit = tmp;

What it does is, getting the settings that I made from the preference activity and I am storing it to a global variable called CommonTool.temperatureUnit. In this case, value "C" is assigned to it.

Fine by now.

In side the CommonTool class, I have a function which returns a temperature in Celsius or Fahrenheit depends on the user preference, I got from the preference activity.

public static String getCommonTemperature(int celsius) {
    int ret;

    if (temperatureUnit == "C") {
        ret = celsius;
    }
    else {
        ret = (9/5) * celsius + 32;
    }

    return Integer.toString(ret);
}

The thing is.. it keeps fall into else part of it while temperatureUnit contains "C".

Capture while in debug mode

Does anyone have any idea? Thanks in advance.

Автор: Harry Источник Размещён: 08.11.2019 10:58

Ответы (1)


0 плюса

Решение

Try this..

public static String getCommonTemperature(int celsius) { int ret;

if (temperatureUnit.equalsIgnoreCase("C")) {
    ret = celsius;
}
else {
    ret = (9/5) * celsius + 32;
}

return Integer.toString(ret);

}

Автор: Silambarasan Poonguti Размещён: 20.08.2016 06:42
Вопросы из категории :
32x32