Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve add custom list of Countries #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions CountryPicker/src/com/countrypicker/Country.java
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
113 changes: 112 additions & 1 deletion CountryPicker/src/com/countrypicker/CountryPicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,16 @@ private List<Country> getAllCountries() {

} catch (Exception e) {
e.printStackTrace();
return allCountriesList;
}
} else {
// Initialize selected countries with all Custom Countries
selectedCountriesList = new ArrayList<Country>();
selectedCountriesList.addAll(allCountriesList);

return allCountriesList;
}
return null;

}

/**
Expand Down Expand Up @@ -257,4 +264,108 @@ public int compare(Country lhs, Country rhs) {
return lhs.getName().compareTo(rhs.getName());
}

/**
* <b>Is not guaranteed to have the country flag.</b> <br>
*
* Set the custom Countries List.
*
* @param customCountriesList
* List the personal Countries.
*/
public void setCountryList(ArrayList<Country> customCountriesList) {
allCountriesList = customCountriesList;
}

/**
* <b>Is not guaranteed to have the country flag.</b> <br>
*
* 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<Country> setCountryListByDefaultLocale() {
ArrayList<Country> countriesList = new ArrayList<Country>();
String[] isoCountries = Locale.getISOCountries();
for (String isoCountry : isoCountries) {
countriesList.add(new Country(new Locale("", isoCountry)
.getDisplayCountry(), isoCountry));
}
allCountriesList = countriesList;
return countriesList;
}

/**
* <b>Is not guaranteed to have the country flag.</b> <br>
*
* 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);
}

/**
* <b>Is not guaranteed to have the country flag.</b> <br>
*
* 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);
}

}
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ picker.setListener(new CountryPickerListener() {

```

When use custom list countries:

```java
ArrayList<Country> countriesList = new ArrayList<Country>();

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)

Expand All @@ -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
See LICENSE.md