【问题标题】:How to create a list of available currencies in Kotlin如何在 Kotlin 中创建可用货币列表
【发布时间】:2019-11-03 13:24:44
【问题描述】:

我想创建当前使用的货币对及其相应显示名称的列表。如果可能的话,我想减少一点(更多内容如下)。

我曾尝试使用 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() 将被使用,这意味着预设货币将是设备的货币,但我想让用户能够更改他的货币(并切换到“设备货币”)。 因此,“次要货币”很可能会被“设备货币”所涵盖,因此,如果可能的话,我想将列表减少到更短的程度,否则可能会导致列表太大,从而导致糟糕的用户体验.

【问题讨论】:

  • 最后有没有找到好的方法。我有同样的问题atm
  • 我已经把这个问题推回去了,还没有找到解决方案,我很可能这个周末会努力,会及时更新!

标签: android list kotlin currency


【解决方案1】:

代替 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)

【讨论】:

  • 很遗憾,这只会返回给定语言环境的可用货币。
猜你喜欢
  • 1970-01-01
  • 2018-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-29
  • 2012-02-22
相关资源
最近更新 更多