how to set default value of the spinnerlist

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-25 02:34:42

问题


In Android I am fetching json data from web.I the list is like {Name,dial,code}

I have this

countryinfo = new ArrayList<CountryInfo>();
        Countrylist = new ArrayList<String>();
            try {           
                for (String line : result) {
                jsonarray= new JSONArray(line);
            for (int i = 0; i < jsonarray.length(); i++) {
                jsonobject = jsonarray.getJSONObject(i);
                CountryInfo conpop = new CountryInfo();
                conpop.setName(jsonobject.optString("Name"));
                conpop.setIso(jsonobject.optString("dial"));
                conpop.setItu(jsonobject.optString("code"));
                countryinfo.add(conpop);
                Countrylist.add(jsonobject.optString("Name"));
                }
                }
        } catch (Exception e) {
            //Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

I use

Spinner mySpinner = (Spinner) findViewById(R.id.spinner1);

             ArrayAdapter<String> adapter = new ArrayAdapter<String>(MobnoAct.this, android.R.layout.simple_spinner_item, Countrylist);  
                            mySpinner.setAdapter(adapter); 
              mySpinner.setSelection(0);

But in the spinner Its showing a default country name..But I want The country name will be as per locale.. like :--

Locale defaultLocale = getResources().getConfiguration().locale;
               String si=defaultLocale.getCountry();

How I can do that???


回答1:


Try this after you set the adapter and retrieve the default local:

 for(String countryName : countryList)
            for(CountryInfo country : countryinfo)
                if(country.getName().equals(countryName) && country.getCode().toLowerCase().equals(si.toLowerCase()))
                    mySpinner.setSelection(adapter.getPosition(countryName));

Hope this helps



来源:https://stackoverflow.com/questions/25725670/how-to-set-default-value-of-the-spinnerlist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!