【问题标题】:Android get all countries on array spinnerAndroid 在数组微调器上获取所有国家/地区
【发布时间】:2020-03-18 20:42:11
【问题描述】:

我搜索了很多,但我发现的东西有点混乱。

我需要获取 android 国家/地区列表并设置默认用户区域设置。

例如: 我正在注册一个用户帐户,我需要插入国家/地区,因为该微调器将显示所有国家/地区,但默认情况下会显示我的默认区域设置。

现在我有:

private Spinner spCountry;
private String array_spinner[];

...

spCountry = (Spinner) findViewById(R.id.spCountry);

array_spinner = new String[1];
array_spinner[0] = "Portugal";

ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, array_spinner);
spCountry.setAdapter(adapter);

谢谢大家的帮助!

【问题讨论】:

    标签: android android-spinner


    【解决方案1】:

    对于我来说,我迭代可用的语言环境并将每个项目添加到 arrayList 中。当然,我必须忽略重复项和空字符串。这是我的代码:

     SortedSet<String> countries = new TreeSet<>();
     for (Locale locale : Locale.getAvailableLocales()) {
         if (!TextUtils.isEmpty(locale.getDisplayCountry())) {
              countries.add(locale.getDisplayCountry());
             }
            }
    
    Spinner citizenship = (Spinner)findViewById(R.id.input_citizenship);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, getCountryListByLocale().toArray(new String[0]));
    citizenship.setAdapter(adapter);
    

    【讨论】:

    • 您也可以使用 Set 代替 List,因此您只需检查空字符串,因为 Set 不能包含重复项。
    【解决方案2】:

    你可以使用

    private static final String DEFAULT_LOCAL = "Portugal";
    

    然后使用它来设置默认选择如下。

    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, array_spinner);
    spCountry.setAdapter(adapter);
    spCountry.setSelection(adapter.getPosition(DEFAULT_LOCAL));
    

    输出:

    更新:res/values中创建arrays.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string-array name="country_arrays">
            <item>Malaysia</item>
            <item>United States</item>
            <item>Indonesia</item>
            <item>France</item>
            <item>Italy</item>
            <item>Singapore</item>
            <item>New Zealand</item>
            <item>India</item>
            <item>Portugal</item>
        </string-array>
    
    </resources>
    

    然后在您的activity 中使用关注来获取所有国家/地区。

    array_spinner = getResources().getStringArray(R.array.country_arrays);
    

    【讨论】:

    • 您好,感谢您的回答,但我的主要问题是:如何让所有国家/地区从 android 到我的微调器?
    • 查看我更新的答案,如果它适合您,则将其标记为答案。
    • 可以使用语言环境来拉取所有国家并显示在数组上吗?硬编码不好(xml)
    • @FilipeOS,这只是一个例子,以便您了解事情是如何运作的。
    • 是的,我明白了,我只是不知道如何推动语言环境国家(全部)并将它们显示在微调器上。谢谢
    【解决方案3】:

    一个有用且可定制的国家选择器,满足您的需求。

    分级

    repositories {
        maven { url "https://jitpack.io" }
    }
    
    compile 'com.github.ekimual:country-picker-x:1.0.0'
    

    示例用法:

    /* Declare */
    CountryPickerDialog countryPicker;
    
    /* Name of your Custom JSON list */
    int resourceId = getResources().getIdentifier("country_avail", "raw", getApplicationContext().getPackageName());
    
    countryPicker = new CountryPickerDialog(MainActivity.this, new  CountryPickerCallbacks() {
          @Override
          public void onCountrySelected(Country country, int flagResId) {
                /* Get Country Name: country.getCountryName(context); */
                /* Call countryPicker.dismiss(); to prevent memory leaks */
          }
    
          /* Set to false if you want to disable Dial Code in the results and true if you want to show it 
             Set to zero if you don't have a custom JSON list of countries in your raw file otherwise use 
             resourceId for your customly available countries */
      }, false, 0);
    
    countryPicker.show();
    

    参考https://android-arsenal.com/details/1/4390

    【讨论】:

      【解决方案4】:

      为什么不这样做呢?只需将国家/地区放入列表中,对其进行排序并插入即可。

      String[] locales = Locale.getISOCountries();
      List<String> countries = new ArrayList<>();
      countries.add("**Select Country**");
      
      for (String countryCode : locales) {
      
              Locale obj = new Locale("", countryCode);
      
              countries.add(obj.getDisplayCountry());
      
      }
      Collections.sort(countries);
      countrySpinner.setItems(countries);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-23
        • 1970-01-01
        • 1970-01-01
        • 2012-12-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多