Frage

Is it possible to get a list of all states when the user selects a particular country from the Locale class of Java ? I was able to get the list of all countries from Locale class. But the Locale class does not provide the states. Is their any other way ? or is there a web service which I could make use of ?

Any help will be appreciated. Thanks in advance :)

War es hilfreich?

Lösung 2

Try this :

    import java.text.Collator;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    import java.util.Locale;

    public class Country  {

    public void getCountries{
    List<Country> countries = new ArrayList<Country>();

    Locale[] locales = Locale.getAvailableLocales();
    for (Locale locale : locales) {
      String iso = locale.getISO3Country();
      String code = locale.getCountry();
      String name = locale.getDisplayCountry();

      if (!"".equals(iso) && !"".equals(code) && !"".equals(name)) {
        countries.add(new Country(iso, code, name));
      }
    }

    Collections.sort(countries, new CountryComparator());
    for (Country country : countries) {
      System.out.println(country);
    }
  }
}

class CountryComparator implements Comparator<Country> {
  private Comparator comparator;

  CountryComparator() {
    comparator = Collator.getInstance();
  }

  public int compare(Country o1, Country o2) {
    return comparator.compare(o1.name, o2.name);
  }
}

class Country {
  private String iso;

  private String code;

  public String name;

  Country(String iso, String code, String name) {
    this.iso = iso;
    this.code = code;
    this.name = name;
  }

  public String toString() {
    return iso + " - " + code + " - " + name.toUpperCase();
  }
}

Andere Tipps

Other than using a google maps API which would probably require you to have a full time connection... There is a dataset with all locals called GeoNames. Which you could use in a SqLite database.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top