2014-06-10 42 views
0

我在数据库中保存了字符串“Locale.CHINA”。 现在,当我做查询我想分配给TextView,货币Locale.CHINA,所以我应该将字符串转换为LOCAL,这有可能吗?Android将字符串转换为LocaleCurrency

NumberFormat ca = NumberFormat.getCurrencyInstance(Locale...); 
String dato = ca.format(valoredouble); 
tv.setText(dato); 

回答

0

你可以做的是将语言和国家作为字符串保存在语言环境中。然后,当您加载该数据时,使用这些字符串Locale (String language, String country)构造您的语言环境数据。 Here's the documentation给你看看。

编辑: 保存用户所选币种的最简单方法之一是SharedPreferences。只需保存如下:

SharedPreferences prefs = getSharedPreferences(“myPrefs”,MODE_PRIVATE); putString(“currency_code”,ca.getCurrency()。getCurrencyCode())。commit();

并加载它是这样的:

SharedPreferences首选项= context.getSharedPreferences( “myPrefs”,MODE_PRIVATE); (Currency.getInstance(prefs.getString(“currency_code”,defaultCurrency)));

+0

我想允许用户选择的币种。我正在考虑不同的解决方案,您认为哪种解决方案最好? – user3608814

+0

加载我已经这么做的货币: 'NumberFormat ca = NumberFormat.getCurrencyInstance(); \t \t String defaultCurrency = Currency.getInstance(Locale.getDefault())。getCurrencyCode(); \t \t ca.setCurrency(sharedPrefs.getString(“code”,defaultCurrency)); ' 但在NumberFormat中不适用于参数(字符串) – user3608814

+0

您使用'Currency.getInstance(String currencyCode)'来解决该问题。如果这有帮助,请通过左边的绿色对号接受答案 – MrEngineer13