我想创建一个当前使用货币对及其相应显示名称的列表。如果可能的话,我想减少一点(在下文中更多)。
我尝试使用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)有没有办法减少 list (目前> 150种货币)
该列表将用于成本跟踪应用程序。标准是将使用NumberFormat.getCurrencyInstance(),这意味着预设的货币将是设备的货币,但我想让用户能够更改其货币(也可以切换到“设备货币”)。
因此,“次要货币”很可能会被“设备货币”所覆盖,因此,如果可能的话,我希望将列表缩小为较短的列表,否则列表可能太大,因此用户体验较差。
最佳答案
您可以传递Locale和日期以代替有效的货币代码,而不是Currency.getAvailableCurrencies()
getAvailableCurrencyCodes
public static String[] getAvailableCurrencyCodes (ULocale loc,
Date d)
哪里
loc ULocale: the locale for which to retrieve currency codes.
d Date: the date for which to retrieve currency codes for the given locale.
只需输入语言环境和日期,它就会返回给您有效的货币代码。
https://developer.android.com/reference/android/icu/util/Currency.html#getAvailableCurrencyCodes(android.icu.util.ULocale,%20java.util.Date)
关于android - 如何在Kotlin中创建可用货币列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58680582/