Android - 从设置ListPreference中指定的值填充Spinner ArrayAdapter

时间:2014-08-08 22:39:28

标签: android android-fragments settings android-preferences listpreference

我的代码应该从我的设置活动中的listpreference中获取指定的值,并使用指定的内容填充我的微调器的arrayadapter。目前我正在尝试在onResume上完成此任务。我的微调器是spLoadFrom。以下是代码:

on片段的onResume显示一个Spinner:

@Override
    public void onResume() {
    super.onResume();
    SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getBaseContext());

    Integer values = getPrefs.getInt("prefSections", 1);

    switch (values) {
        case 1:
            ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, displayName);
            spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
            spLoadFrom.setAdapter(spinnerArrayAdapter);
            SpinnerListener spListener = new SpinnerListener();

            spLoadFrom.setOnItemSelectedListener(spListener);
            break;
        case 2:
            ArrayAdapter<String> spinnerArrayAdapter2 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, displayName2);
            spinnerArrayAdapter2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
            spLoadFrom.setAdapter(spinnerArrayAdapter2);
            SpinnerListener spListener2 = new SpinnerListener();

            spLoadFrom.setOnItemSelectedListener(spListener2);
            break;
        case 3:
            ArrayAdapter<String> spinnerArrayAdapter3 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, displayName3);
            spinnerArrayAdapter3.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
            spLoadFrom.setAdapter(spinnerArrayAdapter3);
            SpinnerListener spListener3 = new SpinnerListener();

            spLoadFrom.setOnItemSelectedListener(spListener3);
            break;
        case 4:
            ArrayAdapter<String> spinnerArrayAdapter4 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, displayName4);
            spinnerArrayAdapter4.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
            spLoadFrom.setAdapter(spinnerArrayAdapter4);
            SpinnerListener spListener4 = new SpinnerListener();

            spLoadFrom.setOnItemSelectedListener(spListener4);
            break;
        case 5:
            ArrayAdapter<String> spinnerArrayAdapter5 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, displayName5);
            spinnerArrayAdapter5.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
            spLoadFrom.setAdapter(spinnerArrayAdapter5);
            SpinnerListener spListener5 = new SpinnerListener();

            spLoadFrom.setOnItemSelectedListener(spListener5);
            break;
        default:
            ArrayAdapter<String> spinnerArrayAdapterd = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, displayName);
            spinnerArrayAdapterd.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
            spLoadFrom.setAdapter(spinnerArrayAdapterd);
            SpinnerListener spListenerd = new SpinnerListener();

            spLoadFrom.setOnItemSelectedListener(spListenerd);
    }
}

我的settings.xml:

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory android:title="@string/prefUpdateCat">
    <CheckBoxPreference
        android:key="prefUpdate"
        android:summary="On/Off"
        android:title="Check for updates" />
</PreferenceCategory>

<PreferenceCategory android:title="@string/prefSectionsCat">
    <ListPreference
        android:key="pref_Sections"
        android:entries="@array/prefSections"
        android:entryValues="@array/prefSectionsValues"
        android:summary="How the 7110.65 should be displayed"
        android:title="7110.65 displayed" />
</PreferenceCategory>

</PreferenceScreen>

我的arrays.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string-array name="prefSections">
    <item name="sec">Split by Section</item>
    <item name="secchap">Split by Section with Chapters</item>
    <item name="secchapwhole">Split by Section with Chapters and the entire document</item>
    <item name="chap">Split by Chapters</item>
    <item name="chapwhole">Split by Chapters and the entire document</item>
</string-array>
<string-array name="prefSectionsValues">
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
    <item>5</item>
</string-array>

</resources>

设置活动:

public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener{

private static final String TAG = "MYAPP";

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);
    PreferenceManager.setDefaultValues(SettingsActivity.this, R.xml.settings, false);
    getActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    Log.d(TAG, "Prefs have changed");

}
}

如何正确加载用户选择的部分的值?

1 个答案:

答案 0 :(得分:0)

我想在进一步浏览StackOverflow之后我找到了问题的答案,但是如果其他人遇到这个问题,我会在这里继续解答。

我必须将values更改为:

Integer values = Integer.parseInt(getPrefs.getString("pref_Sections", ""));

而不是将其作为:

Integer values = getPrefs.getInt("prefSections", 1);

我从ilsy {{1}}查看{{1}}发布的答案时得到了这个想法。