Skip to content Skip to sidebar Skip to footer

Not Able To Sustain Value Of A String Variable After Leaving The Class

I want to sustain countryCode from selected countryName from a spinner dropdown list. This is my code after completing this code I want to sustain countryName and mCountryCode valu

Solution 1:

Request If someone thinks this should have been a comment rather than answer, please don't downvote as I can't add comments being a beginner having low reputation points.

Answer: The problem is that you are using same name (mCountryCode) for two different variables, once as a global variable and then as a local variable for for loop. The value you are assigning in the line just before "break", is actually getting assigned to local variable instead of the global one.

Solution is to just change the name of local variable in teh for loop. Below code segment for "for loop" should work:

Locale locale;
for (String countryCode : isoCountryCodes) {
locale = new Locale("", countryCode);                                
    if(locale.getDisplayCountry().equals(mSpinner.getSelectedItem())){
        mCountryCode = locale.getCountry();
        break;
    }
}

Post a Comment for "Not Able To Sustain Value Of A String Variable After Leaving The Class"