从android操作系统检索国家列表

我正在寻找一种方法来填充一个微调的国家名单与他们的名单。 我可以从Android操作系统检索它吗? 有人可以给我一个例子吗?

你可能会从Locale类中得到一些想法。

调用getAvailableLocales()然后遍历数组& getDisplayCountry() 。 如果这是您第一次看到国家名称,请将其添加到可扩展列表(例如ArrayList实例)。


例如

在Java中,但java.util中的3个类都可以在Android中使用。

 import java.util.*; class Countries { public static void main(String[] args) { Locale[] locales = Locale.getAvailableLocales(); ArrayList countries = new ArrayList(); for (Locale locale : locales) { String country = locale.getDisplayCountry(); if (country.trim().length()>0 && !countries.contains(country)) { countries.add(country); } } Collections.sort(countries); for (String country : countries) { System.out.println(country); } System.out.println( "# countries found: " + countries.size()); } } 

在这个台式电脑上输出

 Albania Algeria Argentina Australia .. Venezuela Vietnam Yemen # countries found: 95 Press any key to continue . . . 

您可以使用Locale.getISOCountries()获取所有两个字母的国家/地区代码的列表。 从两个字母的国家代码中,您可以构建一个Locale对象,然后使用Locale.getDisplayName()来获取国家/地区名称。

 String[] isoCountryCodes = Locale.getISOCountries(); for (String countryCode : isoCountryCodes) { Locale locale = new Locale("", countryCode); String countryName = locale.getDisplayCountry(); } 

在Galaxy Note 3上运行Android 4.3,getISOCountries()会返回246个国家代码,与今天(2014年4月14日)正式分配的ISO国家代码非常接近。

调用getAvailableLocales()只会返回设备的已安装语言环境,而这些语言环境很可能会有更少的不同的国家/地区。

我已经为此创建了一堂课,希望可以帮忙。

 public class CountryDetails { public static String[] country = new String[]{"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "British Indian Ocean Territory", "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burma (Myanmar)", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic", "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands", "Faroe Islands", "Fiji", "Finland", "France", "French Polynesia", "Gabon", "Gambia", "Gaza Strip", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Holy See (Vatican City)", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Isle of Man", "Israel", "Italy", "Ivory Coast", "Jamaica", "Japan", "Jersey", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kosovo", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Montenegro", "Montserrat", "Morocco", "Mozambique", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar", "Republic of the Congo", "Romania", "Russia", "Rwanda", "Saint Barthelemy", "Saint Helena", "Saint Kitts and Nevis", "Saint Lucia", "Saint Martin", "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Korea", "Spain", "Sri Lanka", "Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Timor-Leste", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "US Virgin Islands", "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam", "Wallis and Futuna", "West Bank", "Yemen", "Zambia", "Zimbabwe"}; public static String[] code = new String[]{"+93", "+355", "+213", "+1 684", "+376", "+244", "+1 264", "+672", "+1 268", "+54", "+374", "+297", "+61", "+43", "+994", "+1 242", "+973", "+880", "+1 246", "+375", "+32", "+501", "+229", "+1 441", "+975", "+591", "+387", "+267", "+55", "+246", "+1 284", "+673", "+359", "+226", "+95", "+257", "+855", "+237", "+1", "+238", "+1 345", "+236", "+235", "+56", "+86", "+61", "+891", "+57", "+269", "+682", "+506", "+385", "+53", "+357", "+420", "+243", "+45", "+253", "+1 767", "+1 849", "+1 829", "+1 809", "+593", "+20", "+503", "+240", "+291", "+372", "+251", "+500", "+298", "+679", "+358", "+33", "+689", "+241", "+220", "+970", "+995", "+49", "+233", "+350", "+30", "+299", "+1 473", "+1 671", "+502", "+224", "+245", "+592", "+509", "+379", "+504", "+852", "+36", "+354", "+91", "+62", "+98", "+964", "+353", "+44", "+972", "+39", "+225", "+1 876", "+81", "+44", "+962", "+7", "+254", "+686", "+381", "+965", "+996", "+856", "+371", "+961", "+266", "+231", "+218", "+423", "+370", "+352", "+853", "+389", "+261", "+265", "+60", "+960", "+223", "+356", "+692", "+222", "+230", "+262", "+52", "+691", "+373", "+377", "+976", "+382", "+1 664", "+212", "+258", "+264", "+674", "+977", "+31", "+599", "+687", "+64", "+505", "+227", "+234", "+683", "+672", "+850", "+1 670", "+47", "+968", "+92", "+680", "+507", "+675", "+595", "+51", "+63", "+870", "+48", "+351", "+1", "+974", "+242", "+40", "+7", "+250", "+590", "+290", "+1 869", "+1 758", "+1 599", "+508", "+1 784", "+685", "+378", "+239", "+966", "+221", "+381", "+248", "+232", "+65", "+421", "+386", "+677", "+252", "+27", "+82", "+34", "+94", "+249", "+597", "+268", "+46", "+41", "+963", "+886", "+992", "+255", "+66", "+670", "+228", "+690", "+676", "+1 868", "+216", "+90", "+993", "+1 649", "+688", "+256", "+380", "+971", "+44", "+1", "+598", "+1 340", "+998", "+678", "+58", "+84", "+681", "+970", "+967", "+260", "+263"}; public String[] getCountry() { return country; } public String[] getCode() { return code; } } 

我创建了一个显示所有国家并允许用户选择国家的图书馆( https://github.com/roomorama/AndroidCountryPicker )。 您可以将其嵌入到自己的活动中,或将其显示为对话框。

使用geonames webservices:

  1. 获取国家名单

http://api.geonames.org/countryInfoJSON?username=demo

  1. 根据国家获取状态列表(使用第一个webservice结果的geonameId)

EXA。 对于印度国家,geonameId = 1269750

http://api.geonames.org/childrenJSON?geonameId=1269750&username=demo

  1. 根据状态获取城市列表(使用第2个webservice结果的geonameId)

EXA。 对马哈拉施特拉邦来说,geonameId = 1264418

http://api.geonames.org/childrenJSON?geonameId=1264418&username=demo

这是xml中的国家/地区名称的字符串数组。

  Afghanistan Albania Algeria American Samoa Andorra Angola Antigua and Barbuda Argentina Armenia Aruba Australia Austria Azerbaijan Bahamas Bahrain Bangladesh Barbados Belarus Belgium Belize Benin Bermuda Bhutan Bolivia Bosnia and Herzegovina Botswana Brazil British Virgin Islands Brunei Bulgaria Burkina Faso Burundi Cambodia Cameroon Canada Canary Islands Cape Verde Cayman Islands Central African Republic Ceuta and Melilla Chad Chile China Colombia Comoros Congo [DRC] Congo [Republic] Costa Rica Croatia Cuba Curaçao Cyprus Czech Republic Côte d'Ivoire Denmark Djibouti Dominica Dominican Republic Ecuador Egypt El Salvador Equatorial Guinea Eritrea Estonia Ethiopia Europe Faroe Islands Fiji Finland France French Guiana French Polynesia Gabon Gambia Georgia Germany Ghana Gibraltar Greece Greenland Grenada Guadeloupe Guam Guatemala Guernsey Guinea Guinea-Bissau Guyana Haiti Honduras Hong Kong Hungary Iceland India Indonesia Iran Iraq Ireland Isle of Man Israel Italy Jamaica Japan Jersey Jordan Kazakhstan Kenya Kiribati Kuwait Kyrgyzstan Laos Latin America Latvia Lebanon Lesotho Liberia Libya Liechtenstein Lithuania Luxembourg Macau Macedonia [FYROM] Madagascar Malawi Malaysia Mali Malta Marshall Islands Martinique Mauritania Mauritius Mayotte Mexico Micronesia Moldova Monaco Mongolia Montenegro Morocco Mozambique Myanmar [Burma] Namibia Nepal Netherlands New Caledonia New Zealand Nicaragua Niger Nigeria North Korea Northern Mariana Islands Norway Oman Pakistan Palau Palestine Panama Papua New Guinea Paraguay Peru Philippines Poland Portugal Puerto Rico Qatar Romania Russia Rwanda Réunion Saint Barthélemy Saint Kitts and Nevis Saint Lucia Saint Martin Saint Vincent and the Grenadines Samoa San Marino Saudi Arabia Senegal Serbia Seychelles Sierra Leone Singapore Sint Maarten Slovakia Slovenia Solomon Islands Somalia South Africa South Korea South Sudan Spain Sri Lanka Sudan Suriname Swaziland Sweden Switzerland Syria São Tomé and Príncipe Taiwan Tanzania Thailand Timor-Leste Togo Tonga Trinidad and Tobago Tunisia Turkey Turks and Caicos Islands US Outlying Islands US Virgin Islands Uganda Ukraine United Arab Emirates United Kingdom United States Uruguay Uzbekistan Vanuatu Venezuela Vietnam Western Sahara Yemen Zambia Zimbabwe Åland Islands  

根据国名,我们没有得到正确的国家代码 …所以我已经更正了国家和国家的代码清单…有240左右世界各国…所以有解决方案:)

CountryDetails类: –

 public class CountryDetails { public static String[] country = new String[]{ "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "British Indian Ocean Territory", "British Virgin", "Brunei", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon ", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos Islands", "Colombia", "Comoros", "Cook Islands", "Costa Rica", "Croatia", "Cuba ", "Curacao", "Cyprus ", "Czech Republic", "Democratic Republic of the Congo", "Denmark ", "Djibouti ", "Dominica ", "Dominican Republic", "East Timor", "Ecuador", "Egypt ", "El Salvador", "Equatorial Guinea", "Eritrea ", "Estonia ", "Ethiopia ", "Falkland Islands ", "Faroe Islands ", "Fiji ", "Finland ", "France", "French Polynesia", "Gabon ", "Gambia", "Georgia ", "Germany ", "Ghana ", "Gibraltar", "Greece ", "Greenland", "Grenada ", "Guam ", "Guatemala", "Guernsey ", "Guinea ", "Guinea-Bissau", "Guyana", "Haiti ", "Honduras ", "Hong Kong ", "Hungary ", "Iceland ", "India ", "Indonesia ", "Iran ", "Iraq ", "Ireland ", "Isle of Man ", "Israel", "Italy ", "Ivory Coast", "Jamaica ", "Japan ", "Jersey ", "Jordan ", "Kazakhstan", "Kenya ", "Kiribati", "Kosovo", "Kuwait ", "Kyrgyzstan", "Laos ", "Latvia", "Lebanon ", "Lesotho ", "Liberia ", "Libya ", "Liechtenstein", "Lithuania", "Luxembourg", "Macau ", "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives ", "Mali ", "Malta ", "Marshall Islands ", "Mauritania", "Mauritius ", "Mayotte ", "Mexico ", "Micronesia", "Moldova ", "Monaco ", "Mongolia", "Montenegro", "Montserrat", "Morocco ", "Mozambique", "Myanmar ", "Namibia ", "Nauru ", "Nepal ", "Netherlands", "Netherlands Antilles", "New Caledonia ", "New Zealand ", "Nicaragua ", "Niger ", "Nigeria", "Niue ", "North Korea", "Northern Mariana Islands", "Norway ", "Oman ", "Pakistan", "Palau ", "Palestine", "Panama ", "Papua New Guinea", "Paraguay ", "Peru ", "Philippines ", "Pitcairn ", "Poland ", "Portugal", "Puerto Rico", "Qatar ", "Republic of the Congo", "Reunion ", "Romania ", "Russia", "Rwanda ", "Saint Barthelemy", "Saint Helena", "Saint Kitts and Nevis", "Saint Lucia", "Saint Martin", "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines", "Samoa ", "San Marino", "Sao Tome and Principe", "Saudi Arabia ", "Senegal ", "Serbia ", "Seychelles", "Sierra Leone", "Singapore", "Sint Maarten", "Slovakia", "Slovenia", "Solomon Islands", "Somalia ", "South Africa", "South Korea ", "South Sudan ", "Spain ", "Sri Lanka", "Sudan ", "Suriname", "Svalbard and Jan Mayen ", "Swaziland ", "Sweden ", "Switzerland", "Syria ", "Taiwan", "Tajikistan", "Tanzania ", "Thailand ", "Togo ", "Tokelau ", "Tonga ", "Trinidad and Tobago ", "Tunisia ", "Turkey ", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu ", "US Virgin Islands", "Uganda ", "Ukraine ", "United Arab Emirates", "United Kingdom ", "United States ", "Uruguay ", "Uzbekistan ", "Vanuatu ", "Vatican ", "Venezuela ", "Vietnam ", "Wallis and Futuna", "Western Sahara", "Yemen ", "Zambia", "Zimbabwe"}; public static String[] code = new String[]{ "93", "355", "213", "1-684", "376", "244", "1-264", "672", "1-268", "54", "374", "297", "61", "43", "994", "1-242", "973", "880", "1-246", "375", "32", "501", "229", " 1-441", "975", "591", " 387", "267", "55", "246", "1-284", "673", "359", "226", "257", " 855", "237", "1", "238", " 1-345", "236", "235", " 56", "86", " 61", "61", "57", "269", "682", " 506", "385", "53", "599", "357", "420", " 243", " 45", "253", "1-767", "1-809", "670", "593", "20", "503", "240", "291", "372", "251", "500", "298", "679", "358", "33", "689", "241", "220", "995", "49", "233", " 350", "30", "299", "1-473", "1-671", "502", "44-1481", "224", "245", "592", "509", "504", "852", "36", "354", "91", "62", "98", "964", "353", "44-1624", "972", "39", "225", "1-876", "81", "44-1534", "962", "7", "254", "686", "383", "965", "996", "856", "371", "961", "266", "231", "218", "423", "370", "352", "853", "389", "261", "265", "60", "960", "223", "356", "692", "222", "230", "262", "52", "691", "373", "377", "976", "382", "1-664", "212", "258", "95", "264", "674", "977", "31", "599", "687", "64", "505", "227", "234", "683", "850", "1-670", "47", "968", "92", "680", "970", "507", "675", "595", "51", "63", "64", "48", "351", "1-787", "974", "242", "262", "40", "7", "250", "590", "290", "1-869", "1-758", "590", "508", "1-784", "685", "378", "239", "966", "221", "381", "248", "232", "65", "1-721", "421", "386", "677", "252", "27", "82", "211", "34", "94", "249", "597", "47", "268", "46", "41", "963", "886", "992", "255", "66", "228", "690", "676", "1-868", "216", "90", "993", "1-649", "688", "1-340", "256", "380", "971", "44", "1", "598", "998", "678", "379", "58", "84", "681", "212", "967", "260", "263" }; public static String[] getCountry() { return country; } public static String[] getCode() { return code; } } 

如何使用这些类如下:

  ////GETTING THE COUNTRY AND CODE ARRAY ARE AS FOLLOW:- ArrayList codeArrayList = new ArrayList(Arrays.asList(CountryDetails.getCode())); ArrayList countryArrayList = new ArrayList(Arrays.asList(CountryDetails.getCountry())); /////SETTING THE ADAPTER ACCORDING TO THE COUNTRY NAMES ArrayAdapter adapter = new ArrayAdapter(SignUpScreen.this, android.R.layout.simple_list_item_1, countryArrayList); YOUR_LIST_VIEW.setAdapter(adapter);///HERE YOUR_LIST_VIEW IS YOUR LISTVIEW NAME YOUR_LIST_VIEW.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView adapterView, View view, int i, long l) { Toast.makeText(this,"COUNTRY NAME==>>"+countryArrayList.get(i),Toast.LENGTH_SHORT+" HERE IS THE COUNTY CODE==>>"+codeArrayList.get(i),Toast.LENGTH_SHORT).show(); } }); 

这是一个Kotlin函数,它会给你一个排序列表

 var normalArray = arrayListOf() fun preparedata() { for (countryCode in Locale.getISOCountries()) { val locale = Locale("",countryCode) var countryName: String? = locale.displayCountry if (countryName == null) { countryName = "UnIdentified" } val simpleCountry = Country(countryName,countryCode) normalArray.add(simpleCountry) } normalArray = ArrayList(normalArray.sortedWith(compareBy { it.countryName })) } 

国家是一个数据类

 data class Country(val countryName:String, val countryCode:String){ }