Android Currency.getAvailableCurrencies () before API 19
1 answer
EDIT: I FOUND THE ANSWER
The following code will work for both API> = 19 and API <19. It generates two CharSequence arrays: one contains currency codes in alphabetical order, a description of the corresponding currency of the currency (code + symbol + currency name).
List<String> daConvertire = new ArrayList<String>();
List<String> statiDaConvertire = new ArrayList<String>();
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 19) {
Set<Currency> currencies = Currency.getAvailableCurrencies();
for (Currency currency:currencies) {
try {
String listItem = currency.getCurrencyCode() + " (" + currency.getSymbol() + ")" + " " + currency.getDisplayName();
daConvertire.add(listItem);
statiDaConvertire.add(currency.getCurrencyCode());
}
catch (Exception e) {
}
}
}
else {
ArrayList<String> currencies = new ArrayList<String>();
Locale[] locs = Locale.getAvailableLocales();
for(Locale loc : locs) {
try {
String val=Currency.getInstance(loc).getCurrencyCode();
if(!currencies.contains(val))
currencies.add(val);
} catch(Exception exc)
{
// Locale not found
}
Collections.sort(currencies);
}
for (String currencyCode:currencies) {
try {
Currency currency = Currency.getInstance(currencyCode);
String listItem = currency.getCurrencyCode() + " (" + currency.getSymbol() + ")" + " " + currency.getDisplayName();
daConvertire.add(listItem);
statiDaConvertire.add(currency.getCurrencyCode());
}
catch (Exception e) {
}
}
}
CharSequence[] entries = daConvertire.toArray(new CharSequence[daConvertire.size()]);
CharSequence[] stati = daConvertire.toArray(new CharSequence[statiDaConvertire.size()]);
+6
source to share