离开课程后无法维持字串变数的值 [英] Not able to Sustain value of a string variable after leaving the class

查看:104
本文介绍了离开课程后无法维持字串变数的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从微调框下拉列表中保留所选countryName中的countryCode.这是我的代码,完成此代码后,我想保留countryName和mCountryCode值并将其带到新活动以在JSON对象中使用它.我已经从语言环境对象中获取了国家代码,并将其放入了一个arraylist国家名称中,以填充微调框.用户选择国家/地区名称后,我希望所选的国家/地区名称再次成为国家/地区代码,并将其存储在字符串值中.一切正常,直到断线.字符串countryCode中也存在选定的countryName,但是在我离开该类之后,mCountryCode类的值不存在.

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 value and take it to new activity to use it in JSON object. I have got country code from locale object and put it in an arraylist country name to populate the spinner. After user selects country name I want that selected country name to be country code again and store it in a string value. All works fine till break line. Selected countryName is there in string countryCode is also there but after I leave the class mCountryCode value is not there.

我认为变量作用域是我需要研究的东西...

I think variable scope is something I need to work on...

public class MyActivity extends AppCompatActivity{
    String mCountryCode;
        onCreate{
        final String[] isoCountryCodes = Locale.getISOCountries();
        //filling spinner object with countryName array using isoCountryCodes array
        countryName.add("Select A country");
        for (String countryCode : isoCountryCodes) {
            Locale locale = new Locale("", countryCode);
            countryName.add(locale.getDisplayCountry());
        }
       //spinner object has been set with array adapter and that works fine below is how to 
       //handle selected countryName and convert it to countryCode again and sustain its value 
       //in a string variable so along with countryName, the corresponding countryCode can be sent via JSON object...

        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            mCountryName = mSpinner.getSelectedItem().toString();

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

推荐答案

请求 如果有人认为这应该是评论,而不是回答,请不要投票,因为我不能添加评论,因为它是信誉较低的初学者.

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.

答案: 问题在于,您对两个不同的变量使用了相同的名称(mCountryCode),一次用作全局变量,然后用作for循环的局部变量.您在"break"之前的行中分配的值实际上已分配给局部变量,而不是全局变量.

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.

解决方案是仅在teh for loop中更改局部变量的名称.下面的"for循环"代码段应该工作:

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;
    }
}

这篇关于离开课程后无法维持字串变数的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆