API之前的Android Currency.getAvailableCurrencies()

时间:2014-12-11 12:07:18

标签: android currency

方法getAvailableCurrencies仅在API 19(Kitkat)之后才可用。关于如何在以前的API中实现Currency.getAvailableCurrencies的相同结果的任何想法?

(我正致力于API 15)

1 个答案:

答案 0 :(得分:6)

编辑:我发现了答案

以下代码适用于API> = 19和API< 19.它生成两个CharSequence Arrays:一个包含按字母顺序排列的货币代码,相应货币的描述(代码+符号+货币名称)。

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()]);