Android - 在我的应用程序首选项中实现ListPreferences

时间:2014-08-06 19:27:25

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

如果在我的偏好设置中从Fragment进行选择,我将如何更改ListPreferences中微调器数组的内容?

在我的应用中,我尝试添加包含ListPreferences的首选项。我编写了Activity设置,它显示了我的数组列表,但是当我选择一个选项时,我的应用程序中没有任何变化。我的应用从主Fragments运行Activity。我尝试使用用户选择的设置操作从我的初始Fragment显示的数据。这是我的代码:

SettingsActivity:

@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);
    //ListPreference lp = (ListPreference)findPreference("prefSections");
    //lp.setEntries(new String [] {"prefSections", "PrefSectionsValues"});
    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) {

}

最初显示的片段:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    container.removeAllViews();

    //Use View "v" containing the layout and container - use v.findViewById in others
    View v = inflater.inflate(R.layout.mylayout, container, false);

    SharedPreferences settingPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getBaseContext());
    String settingSection = settingPrefs.getString("prefSectionsValues", "1");
        if (settingSection == "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);
    } else if (settingSection == "2"){
        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, displayName2);
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spLoadFrom.setAdapter(spinnerArrayAdapter);
    } else if (settingSection == "3"){
        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, displayName3);
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spLoadFrom.setAdapter(spinnerArrayAdapter);
    }else if (settingSection == "4"){
        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, displayName4);
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spLoadFrom.setAdapter(spinnerArrayAdapter);
    }else if (settingSection == "5"){
        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, displayName5);
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spLoadFrom.setAdapter(spinnerArrayAdapter);
    }
    //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);
    // Inflate the layout for this fragment
return v;
}

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 doc</item>
    <item name="chap">Split by Chapters</item>
    <item name="chapwhole">Split by Chapters and the entire doc</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>

的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 it should be displayed"
        android:title="it's displayed..." />
</PreferenceCategory>

</PreferenceScreen>

1 个答案:

答案 0 :(得分:1)

尝试更改您的SettingsActivity类以实现SharedPreferences.OnSharedPreferenceChangeListener,以便在注册用户首选项更改时让您做出适当的响应。

public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
    ...
    ...
}


@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        String key) {
    Log.d(TAG, "Preferences have been changed");
    //indicate fragment needs to update it's content
}

此外,您的片段似乎没有得到更新,因为您显示用户首选项的逻辑位于oncreateview函数中,该函数仅在创建该片段时被调用。 Read more about that here。基本上,onResume函数将是一个更好的地方,而不是onCreate。只是不要在onCreate和onResume中复制你的代码,否则它会在你的片段第一次创建时运行两次。

Override
public void onResume() 
{ 
    super.onResume();
    //if spinnerarrayadapter is empty then populate as normal
         //set as adapter for spLoadForm
    //else if user preferences have changed
         //populate spinnerarrayadapter here; 
         //spinnerarrayadapter.notifyDataSetChanged();
}

您可以使用在onSharedPreferenceChange()函数中设置的静态全局变量来跟踪用户首选项是否已更改。虽然(或者如果你不认为使用内容填充spinnerarrayadapter是一个在最终版本产品中多次运行会影响最终用户体验的缓慢过程),你可以将其作为else语句。

希望这有帮助。