Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何为用户提供选择货币格式的选项_Android - Fatal编程技术网

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

Android 如何为用户提供选择货币格式的选项,android,Android,我想让用户选择货币显示格式,如本例所示: 我面临着几个挑战。首先,我不知道如何传递 Currency.getAvailableCurrencies(); 到ListPreference项和值集。下面是我的尝试 首先是xml <ListPreference android:key="currency" android:title="Currency" android:defaultValue="$"

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

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

Currency.getAvailableCurrencies();
到ListPreference项和值集。下面是我的尝试

首先是xml

<ListPreference
           android:key="currency"
           android:title="Currency"
           android:defaultValue="$"
           android:negativeButtonText="@null"
           android:positiveButtonText="@null" />
那么,如何在ListPreference> 更新-工作代码

对于正在阅读的任何人,以下是根据以下答案更新的工作代码,不确定我现在将如何处理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();
            }
        }
    }
公共静态类设置片段扩展了PreferenceFragment{
@TargetApi(Build.VERSION\u code.KITKAT)
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//添加首选项xml
addPreferencesFromResource(R.xml.pref_-general);
//获取根首选项屏幕
PreferenceScreen mPreferenceScreen=(PreferenceScreen)getPreferenceScreen();
//获取货币列表首选项
ListPreference listPref=(ListPreference)mPreferenceScreen.FindReference(“货币”);
if(listPref!=null){
//获取可用的货币集
设置货币=货币。GetAvailableCurrences();
CharSequence[]entries=新的CharSequence[Currences.size()];
CharSequence[]值=新的CharSequence[currences.size()];
int i=0;
用于(货币:货币){
String tempCurrency=String.format(“%s\t%s\t%s”,currency.getDisplayName(),currency.getSymbol(),currency.toString());
如果(!tempCurrency.trim().isEmpty()){
分录[i]=临时货币;
值[i]=currency.getSymbol();
}
i++;
}
listPref.setEntries(条目);
listPref.setDefaultValue($);
listPref.setEntryValues(值);
}否则{
Toast.makeText(getActivity(),“ListPreference为空”,Toast.LENGTH_SHORT.show();
}
}
}

这一行似乎是问题所在,我不知道为什么您认为它会自动将货币对象转换为字符串

//Convert the currency Set<E> to String[] so I can get Array contents 
        String[] currencyArray = currencySet.toArray(new String[currencySet.size()]);
//将货币集转换为字符串[],以便获取数组内容
String[]currencyArray=currencySet.toArray(新字符串[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.

    }
Set currences=Currency.getAvailableCurrences();
用于(货币:货币){
System.out.printf(“%s\t%s\t%s\n”、currency.getDisplayName()、currency.getSymbol()、currency.toString());
//您的代码用于检查此处的符号是否为空
//将其添加到字符串数组或直接使用
//CharSequences数组中的条目和值。
}
//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.

    }