[안드로이드] Android 국가코드 얻어오기 및 정렬
안녕하세요 푸민입니다.
KR 이 한국을 표시하는 것처럼 각 국가 코드가 필요할 때가 있습니다. 안드로이드에서 국가 코드를 어떻게 불러오는지 알아볼까요?
1. 모든 국가 코드 확인하기
- 안드로이드에서 제공하는 모든 국가 코드를 불러옵니다.
Locale[] availableLocales = Locale.getAvailableLocales();
for (Locale locale : availableLocales) {
String code = locale.getCountry();
String name = locale.getDisplayCountry();
String ename = locale.getDisplayCountry(Locale.ENGLISH);
Log.d(TAG, code + ", " + ename + ", " + name);
}
2. 국가 코드 정렬하기
- 불러온 국가 코드를 정렬합니다.
Locale[] availableLocales = Locale.getAvailableLocales();
ArrayList<Locale> list = new ArrayList<Locale>();
for (Locale locale : availableLocales) {
String code = locale.getCountry();
String name = locale.getDisplayCountry();
String ename = locale.getDisplayCountry(Locale.ENGLISH);
Log.d(TAG, code + ", " + ename + ", " + name);
list.add(locale);
}
Collections.sort(list, new Sortt());
Log.d(TAG, "------------------------");
for (Locale locale : list) {
String code = locale.getCountry();
String name = locale.getDisplayCountry();
String ename = locale.getDisplayCountry(Locale.ENGLISH);
Log.d(TAG, code + ", " + ename + ", " + name);
}
class Sortt implements Comparator<Locale>{
@Override
public int compare(Locale lhs, Locale rhs) {
return lhs.getDisplayCountry(Locale.ENGLISH).compareTo(rhs.getDisplayCountry(Locale.ENGLISH));
}
}