diff --git a/CountryPicker/src/com/countrypicker/Country.java b/CountryPicker/src/com/countrypicker/Country.java index 10476a7..1312671 100644 --- a/CountryPicker/src/com/countrypicker/Country.java +++ b/CountryPicker/src/com/countrypicker/Country.java @@ -1,13 +1,18 @@ package com.countrypicker; -/** - * POJO - * - */ public class Country { + private String code; private String name; + public Country() { + } + + public Country(String name, String code) { + this.name = name; + this.code = code; + } + public String getCode() { return code; } diff --git a/CountryPicker/src/com/countrypicker/CountryPicker.java b/CountryPicker/src/com/countrypicker/CountryPicker.java index cc19e3f..5cf347f 100644 --- a/CountryPicker/src/com/countrypicker/CountryPicker.java +++ b/CountryPicker/src/com/countrypicker/CountryPicker.java @@ -124,9 +124,16 @@ private List getAllCountries() { } catch (Exception e) { e.printStackTrace(); + return allCountriesList; } + } else { + // Initialize selected countries with all Custom Countries + selectedCountriesList = new ArrayList(); + selectedCountriesList.addAll(allCountriesList); + + return allCountriesList; } - return null; + } /** @@ -257,4 +264,108 @@ public int compare(Country lhs, Country rhs) { return lhs.getName().compareTo(rhs.getName()); } + /** + * Is not guaranteed to have the country flag.
+ * + * Set the custom Countries List. + * + * @param customCountriesList + * List the personal Countries. + */ + public void setCountryList(ArrayList customCountriesList) { + allCountriesList = customCountriesList; + } + + /** + * Is not guaranteed to have the country flag.
+ * + * This method create list of Countries based on "ISO 3166 country codes + * that can be used as the country code when constructing a Locale". + * + * @see java.util.Locale#getISOCountries() + * @see java.util.Locale#getDisplayCountry() + * + * @return List of Countries created (use if necessary). + */ + public ArrayList setCountryListByDefaultLocale() { + ArrayList countriesList = new ArrayList(); + String[] isoCountries = Locale.getISOCountries(); + for (String isoCountry : isoCountries) { + countriesList.add(new Country(new Locale("", isoCountry) + .getDisplayCountry(), isoCountry)); + } + allCountriesList = countriesList; + return countriesList; + } + + /** + * Is not guaranteed to have the country flag.
+ * + * Adds the specified Country at the end of this List. + * + * @param country + * Country to add. + * + * @throws UnsupportedOperationException + * if adding to this List is not supported. + * + * @throws ClassCastException + * if the class of the object is inappropriate for this List + * + * @throws IllegalArgumentException + * if the object cannot be added to this List. + */ + public void addCountry(Country country) { + allCountriesList.add(country); + } + + /** + * Is not guaranteed to have the country flag.
+ * + * Inserts the Country into this List at the specified location. The Country + * is inserted before the current element at the specified location. If the + * location is equal to the size of this List, the object is added at the + * end. If the location is smaller than the size of this List, then all + * elements beyond the specified location are moved by one position towards + * the end of the List. + * + * @param location + * the index at which to insert. + * @param country + * Country to add. + * + * @throws UnsupportedOperationException + * if adding to this List is not supported. + * + * @throws ClassCastException + * if the class of the object is inappropriate for this List + * + * @throws IllegalArgumentException + * if the object cannot be added to this List. + * + * + * @throws IndexOutOfBoundsException + * if location < 0 || location > size() + */ + public void addCountry(int location, Country country) { + allCountriesList.add(location, country); + } + + /** + * /** Removes the object at the specified location from this List. + * + * @param location + * the index of the object to remove. + * @return the removed object. + * + * @throws UnsupportedOperationException + * if removing from this List is not supported + * + * @throws IndexOutOfBoundsException + * if location < 0 || location >= size() + */ + public Country remove(int location) { + return allCountriesList.remove(location); + } + } diff --git a/README.md b/README.md index a148e8c..68f16a0 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,24 @@ picker.setListener(new CountryPickerListener() { ``` +When use custom list countries: + +```java +ArrayList countriesList = new ArrayList(); + +countriesList.add(new Country("United States", "US")); +countriesList.add(new Country("Brazil", "BR")); +// add Countries to list. + +picker.setCountryList(customCountriesList); +``` + +When use list of Countries ( `java.util.Locale.getISOCountries()`) based on ISO 3166 country codes that can be used as the country code when constructing a Locale: + +```java +picker.setCountryListByDefaultLocale(); +``` + ## About The data is from CountryPicker by nicklockwood (https://github.com/nicklockwood/CountryPicker) @@ -56,4 +74,4 @@ I converted his data in "Countries.plist" to json format to avoid extra dependen Thanks Nick for his awesome library! ## License -See LICENSE.md \ No newline at end of file +See LICENSE.md