如何为用户提供选择货币格式的选项

时间:2015-05-25 01:11:18

标签: android

我想让用户选择货币显示格式,如下例所示:

Android multiple currency display settings

我面临的挑战。首先,我不知道如何传递

的值
Currency.getAvailableCurrencies();

到ListPreference条目和值集。以下是我的尝试。

首先是xml

<ListPreference
           android:key="currency"
           android:title="Currency"
           android:defaultValue="$"
           android:negativeButtonText="@null"
           android:positiveButtonText="@null" />

然后我尝试从代码

填充listPreference
public static class SettingsFragment extends PreferenceFragment {
    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Add preference xml
        addPreferencesFromResource(R.xml.pref_general);

        //Get root PreferenceScreen
        PreferenceScreen mPreferenceScreen = getPreferenceManager().createPreferenceScreen(getActivity());

        //Get the currency list preference
        ListPreference listPref = (ListPreference) mPreferenceScreen.findPreference("currency");

        //Get available currency set
        Set<Currency> currencySet = Currency.getAvailableCurrencies();

        //Convert the currency Set<E> to String[] so I can get Array contents
        String[] currencyArray = currencySet.toArray(new String[currencySet.size()]);

        CharSequence[] entries = new CharSequence[currencyArray.length];
        CharSequence[] values = new CharSequence[currencyArray.length];

        for (int i = 0; i < entries.length; i++){
            entries[i] = currencyArray[i].toString();
            values[i] = currencyArray[i].toString();
        }

        listPref.setEntries(entries);
        listPref.setEntryValues(values);
    }
}

以上失败,但有以下例外

Caused by: java.lang.ArrayStoreException: source[0] of type java.util.Currency cannot be stored in destination array of type java.lang.String[]

那么如何在ListPreference中提供可读货币的可读列表&gt; 更新 - 工作代码

对于阅读的任何人来说,以下是根据以下答案更新的工作代码,不确定此时我将如何处理Pre KitKat设备

public static class SettingsFragment extends PreferenceFragment {
        @TargetApi(Build.VERSION_CODES.KITKAT)
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            //Add preference xml
            addPreferencesFromResource(R.xml.pref_general);

            //Get root PreferenceScreen
            PreferenceScreen mPreferenceScreen = (PreferenceScreen)getPreferenceScreen();

            //Get the currency list preference
            ListPreference listPref = (ListPreference) mPreferenceScreen.findPreference("currency");

            if (listPref != null) {
                //Get available currency set
                Set<Currency> currencies = Currency.getAvailableCurrencies();


                CharSequence[] entries = new CharSequence[currencies.size()];
                CharSequence[] values = new CharSequence[currencies.size()];

                int i = 0;
                for (Currency currency: currencies){
                    String tempCurrency = String.format("%s\t%s\t%s",currency.getDisplayName(), currency.getSymbol(), currency.toString());
                    if (!tempCurrency.trim().isEmpty()){
                        entries[i] = tempCurrency;
                        values[i] = currency.getSymbol();
                    }
                    i++;
                }

                listPref.setEntries(entries);
                listPref.setDefaultValue("$");
                listPref.setEntryValues(values);

            } else {
                Toast.makeText(getActivity(), "ListPreference is null", Toast.LENGTH_SHORT).show();
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

这一行看起来像是问题,我不知道为什么你认为它会自动将Currency对象转换为字符串

//Convert the currency Set<E> to String[] so I can get Array contents 
        String[] currencyArray = currencySet.toArray(new String[currencySet.size()]);

您希望将其替换为:

    Set<Currency> currencies = Currency.getAvailableCurrencies();
    for (Currency currency: currencies) {
        System.out.printf("%s\t%s\t%s\n",currency.getDisplayName(), currency.getSymbol(), currency.toString());
        // your code to check whether the symbol is not empty here
        // add it to your String array or just directly use the
        // CharSequences arrays for entries and values here.

    }