将以前存储的数据从SharedPreferences加载到PreferenceFragment中

时间:2016-01-03 19:09:59

标签: android fragment settings preferences

我已按照文档创建了一个Preference Fragment。我的所有首选项都按预期工作,但我有一个名字设置,我想从SharedPreferences动态加载。用户登录时已存储这些值。

他们在strings.xml中的值:

 <string name="first_name">first_name</string>
 <string name="last_name">last_name</string>

他们在登录时保存:

editor.putString(mContext.getResources().getString(R.string.first_name), firstName);
editor.putString(mContext.getResources().getString(R.string.last_name), lastName);
editor.commit();

preferences.xml中的首选项:

   <EditTextPreference
                android:key="@string/first_name"
                android:inputType="text"
                android:persistent="true"
                android:title="First Name"/>
    <EditTextPreference
                android:key="@string/last_name"
                android:inputType="text"
                android:persistent="true"
                android:title="Last Name"/>

尝试在Preference Fragment中加载它们:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sessionManager = SessionManager.getInstance(getActivity());
        addPreferencesFromResource(R.xml.preferences);
        PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);

        // Set first and last name preference default values
        firstNamePreference = (EditTextPreference)findPreference("first_name");
        lastNamePreference = (EditTextPreference)findPreference("last_name");
        nearbyPreference = (CheckBoxPreference)findPreference("show_places");
//        firstNamePreference.setText(sessionManager.getUserFirstName());
//        firstNamePreference.setSummary(sessionManager.getUserFirstName());
//        lastNamePreference.setText(sessionManager.getUserLastName());
//        lastNamePreference.setSummary(sessionManager.getUserLastName());
        nearbyPreference.setDefaultValue(true);
    }

所以我可以手动设置它们(以及摘要),但我真的想从SharedPreferences中已经设置的值设置它们。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

因此,在查看我的代码之后,我意识到我的应用程序将首选项存储在默认文件以外的文件中。所以,我必须从this文章中做到以下几点:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);


        sharedPreferences = getActivity().getSharedPreferences("file_name", 0); // 0 is private

        // show the current value in the settings screen
        for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) {
            pickPreferenceObject(getPreferenceScreen().getPreference(i));
        }
    }

private void pickPreferenceObject(Preference p) {
        if (p instanceof PreferenceCategory) {
            PreferenceCategory cat = (PreferenceCategory) p;
            for (int i = 0; i < cat.getPreferenceCount(); i++) {
                pickPreferenceObject(cat.getPreference(i));
            }
        } else {
            initSummary(p);
        }
    }

private void initSummary(Preference p) {
        if (p instanceof EditTextPreference) {
            EditTextPreference editTextPref = (EditTextPreference) p;
            String value = sharedPreferences.getString(editTextPref.getKey(), "");
            p.setSummary(value);
            ((EditTextPreference) p).setText(value);
        }
        // More logic for ListPreference, etc...
    }

否则你可能只需用默认的prefs设置它们,就像这样访问:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());

答案 1 :(得分:0)

这样的事情就是你应该做的事情:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);

    // Sets the first name based on the SharedPreference value
    Preference pref = getPreferenceScreen().findPreference("first_name");
    pref.setSummary(pref.getSharedPreferences().getString("first_name", "default value if first_name key not set"));

}