2013-09-03 22 views
10

Tôi đã tìm kiếm rất nhiều nhưng nội dung tôi thấy hơi bối rối.Android có được tất cả các quốc gia trên mảng spinner

Tôi cần tải danh sách quốc gia của Android và đặt ngôn ngữ người dùng mặc định. Ví dụ: Tôi đang đăng ký một tài khoản người dùng và tôi cần phải chèn quốc gia, trong spinner đó sẽ hiển thị tất cả các quốc gia nhưng theo mặc định sẽ xuất hiện ngôn ngữ mặc định của tôi.

Ngay bây giờ tôi có:

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

Cảm ơn tất cả các bạn đã giúp đỡ!

Trả lời

27

Đối với tôi, tôi lặp lại Locales khả dụng và thêm từng mục vào danh sách mảng. Và tất nhiên tôi phải bỏ qua các bản sao và chuỗi rỗng. Đây là mã của tôi:

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

Bạn cũng có thể sử dụng Cài đặt thay cho Danh sách, vì vậy, bạn chỉ phải kiểm tra các chuỗi trống vì Tập hợp không thể chứa các mục trùng lặp. –

6

Bạn có thể sử dụng

private static final String DEFAULT_LOCAL = "Portugal"; 

Sau đó sử dụng nó để thiết lập lựa chọn mặc định như sau.

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

OUTPUT:

enter image description here

UPDATE: Tạo arrays.xml trong res/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> 

Sau đó, sử dụng sau bạn activity để có được tất cả các nước.

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

Xin chào, cảm ơn câu trả lời của bạn nhưng vấn đề chính của tôi là: Làm cách nào để mọi quốc gia từ android đến spinner của tôi? – FilipeOS

+0

xem câu trả lời cập nhật của tôi, nếu câu trả lời phù hợp với bạn thì hãy đánh dấu câu trả lời. –

+0

Có thể sử dụng ngôn ngữ để kéo tất cả các quốc gia và hiển thị trên mảng phải không? Hardcoded nó là xấu (xml) – FilipeOS

0

Bộ chọn quốc gia hữu ích và có thể tùy chỉnh cho nhu cầu của bạn.

Gradle

repositories { 
    maven { url "https://jitpack.io" } 
} 

compile 'com.github.ekimual:country-picker-x:1.0.0' 

Sample Cách sử dụng:

/* 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(); 

Reference https://android-arsenal.com/details/1/4390

0

Tại sao không làm việc đó như thế này? Chỉ cần nhận các quốc gia trong một danh sách, phân loại và chèn nó vào.

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); 
Các vấn đề liên quan