如何获得可用的国家和货币?

时间:2016-05-03 13:15:11

标签: android locale currency

我尝试同时获取所有可用国家/地区和货币的列表。 我希望国家名称在微调器上,当用户选择国家时,它会返回货币符号。 我的方法有效,但我认为这是非常糟糕的做法,因为加载数据需要3到2秒。 我是这样做的:

  public ArrayList<CurrencyModel> getCountriesAndCurrency(){
        ArrayList<CurrencyModel> result = new ArrayList<>();
        ArrayList<Locale> list = new ArrayList<>(Arrays.asList(Locale.getAvailableLocales()));
        for (int i = 0; i <list.size(); i++) {
            String country = list.get(i).getDisplayCountry();
            try {
                Locale pickedLocal = new Locale(list.get(i).getISO3Language(), list.get(i).getCountry());
                String code = Currency.getInstance(pickedLocal).getCurrencyCode();
                Currency currency = Currency.getInstance(code);
                String currencySymbol = currency.getSymbol(pickedLocal);
                if(currencySymbol.length()>1){
                    currencySymbol = currency.getSymbol();
                }
                result.add(new CurrencyModel(list.get(i), country, currencySymbol));
            } catch (Exception e) {
            }
        }
        Collections.sort(result, new CustomComparator());
        ArrayList<CurrencyModel> finalRes = new ArrayList<>();
        for (int i = 0; i < result.size(); i++) {
            try {
                if(!result.get(i).getCountryName().equals(result.get((i+1)).getCountryName())) {
                    finalRes.add(result.get(i));
                }
            }catch (Exception e){}
        }
        Collections.sort(finalRes, new CustomComparator());
        return finalRes;
    }

    public class CustomComparator implements Comparator<CurrencyModel> {
        @Override
        public int compare(CurrencyModel o1, CurrencyModel o2) {
            return o1.getCountryName().compareTo(o2.getCountryName());
        }
    }

CurrencyModel对象是= locale locale,String countryName,String currencySymbol。

这个行动有更好的解决方案吗?

1 个答案:

答案 0 :(得分:1)

发现有几件事可以帮助你 不确定是否会显着提高效果

1- Locale重新实例化 Locale pickedLocal = new Locale(list.get(i).getISO3Language(), list.get(i).getCountry()); 为什么要使用locale属性重新创建区域设置? 你只需要分配它:

Locale pickedLocal = list.get(i);

2-货币相同,

String code = Currency.getInstance(pickedLocal).getCurrencyCode();
Currency currency = Currency.getInstance(code);
String currencySymbol = currency.getSymbol(pickedLocal);

据我所知,你的目标是currencySymbol,所以就这样做:

String currencySymbol = Currency.getInstance(pickedLocal).getSymbol();

所以最后一段代码(带有try-catch的代码)看起来应该是这样的

try {
    Locale pickedLocal = list.get(i);
    String currencySymbol = Currency.getInstance(pickedLocal).getSymbol();
    if(currencySymbol.length()>1){
        currencySymbol = currency.getSymbol();
    }
    result.add(new CurrencyModel(pickedLocal, country, currencySymbol));
} catch (Exception e) {
}

result.add(new CurrencyModel(list.get(i),...使用pickedLocal,不要从列表中重新获取项目。

这是我到目前为止所得到的,我将追加任何进一步的答案。

编辑:(也许你必须对邮件进行排序&#34;列表&#34;首先) 第二次查看代码,我发现你在填充结果列表后尝试删除重复项,如果你在添加新项目之前检查结果列表,你将摆脱第二个循环

try {
    Locale pickedLocal = list.get(i);
    String currencySymbol = Currency.getInstance(pickedLocal).getSymbol();
    if(currencySymbol.length()>1){
        currencySymbol = currency.getSymbol();
    }

    //TO AVOID the second loop (check duplicates), add only if not exist
    final int size = result.size();
    if(size > 0){
        if(!result.get(size-1).getCountryName().equals(country)){
            //no similar item was added yet, so add the new item
            result.add(new CurrencyModel(pickedLocal, country, currencySymbol));
        }
    }else{
        //it still empty, just add the new item
        result.add(new CurrencyModel(pickedLocal, country, currencySymbol));
    }
} catch (Exception e) {
}