如何设置spinnerlist的默认值 [英] how to set default value of the spinnerlist

查看:173
本文介绍了如何设置spinnerlist的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的我从web.I获取JSON数据列表就像{名称,拨号,code}

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

我有这种

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

我用

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

但在微调其显示name..But我希望国家名称将根据区域设置默认的国家..
如: -

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???

推荐答案

试试这个后适配器设置并检索本地默认:

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

希望这有助于

这篇关于如何设置spinnerlist的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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