我想创建当前使用的货币对及其相应显示名称的列表。如果可能的话,我想减少一点(在以下内容中更多)。
我已经尝试使用Currency.getAvailableCurrencies(),但它返回不再使用的货币。我现在最好的解决方案是使用Locale.getAvailableLocales()并从那里获取货币(请参见以下代码)。
我正在使用SortedSet删除重复项(在语言环境列表中有很多重复项)并对其进行排序。该对包括显示名称和货币符号。
val filteredListOfCurrencies: SortedSet<Pair<String?,String?>> = listOfLocale.map{Currency.getInstance(it)?.displayName to Currency.getInstance(it)?.symbol}.toSortedSet(
compareBy{it.first})
我的问题是:1)有更好的方法吗?2)有没有办法减少清单(当前> 150种货币)
该列表将用于费用跟踪应用。标准是将使用NumberFormat.getCurrencyInstance(),这意味着预设的货币将是设备的货币,但我想让用户能够更改其货币(也可以切换到“设备货币”)。因此,“次要货币”很可能会被“设备货币”所覆盖,因此,如果可能的话,我希望将列表缩小为较短的列表,否则列表可能太大,因此用户体验较差。
谢谢您的任何建议。
代替Currency.getAvailableCurrencies(),您可以传递语言环境和日期以检索有效的货币代码
getAvailableCurrencyCodes
public static String[] getAvailableCurrencyCodes (ULocale loc,
Date d)
where
loc ULocale: the locale for which to retrieve currency codes.
d Date: the date for which to retrieve currency codes for the given locale.
只需输入语言环境和日期,它将返回给您有效的货币代码。