web-dev-qa-db-ja.com

Androidアレイスピナーですべての国を取得

たくさん検索しましたが、見つけたものは少し混乱していました。

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

助けてくれてありがとう!

12
FilipeOS

私については、利用可能なロケールを反復処理し、各項目をarrayListに追加します。そしてもちろん、重複や空の文字列は無視する必要があります。これが私のコードです:

Locale[] locale = Locale.getAvailableLocales();
ArrayList<String> countries = new ArrayList<String>();
String country;
for( Locale loc : locale ){
    country = loc.getDisplayCountry();
    if( country.length() > 0 && !countries.contains(country) ){
        countries.add( country );
    }
}
Collections.sort(countries, String.CASE_INSENSITIVE_ORDER);

Spinner citizenship = (Spinner)findViewById(R.id.input_citizenship);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,Android.R.layout.simple_spinner_item, countries);
citizenship.setAdapter(adapter);
33
sanusi

使用できます

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

出力:

enter image description here

PDATE:作成arrays.xmlres/values

<?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);
7
Ritesh Gune

あなたのニーズに役立つ、カスタマイズ可能なカントリーピッカー。

Gradle

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/439

1
Manoj ahirwar

このようにしてみませんか?国をリストに入れ、並べ替えて挿入するだけです。

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);
0
Hasan Sawan