0

当我查看从 get all 返回的地图时,我只能看到之前设置为 hashmap 的值,并且无法转换为 EditTextPreference。

它看起来像 getAll() 正在获取首选项哈希,但不是来自 XML 的类型

我想要做的是将摘要设置为对值的偏好。

public static class Prefs1Fragment extends PreferenceFragment {

    SharedPreferences sharedPreferences;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.initial_preferences);
    }

    @Override
    public void onResume() {
        super.onResume();
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());

        Map<String, ?> preferencesMap = sharedPreferences.getAll();
        String appId = sharedPreferences.getString(PabuloConstants.InternalAppIdPerfName, "N/A");
        // iterate through the preference entries and update their summary if they are an instance of EditTextPreference
        for (Map.Entry<String, ?> preferenceEntry : preferencesMap.entrySet()) {
            if(preferenceEntry instanceof EditTextPreference){
                Log.d("","found instance");
            }
        }
    }
}

XML:

<PreferenceCategory
    android:title="Category Title">

    <EditTextPreference
        android:key="app_id_preference"
        android:title="some title"
        android:summary="some summary"
        />

    <EditTextPreference
        android:key="app_id_preference2"
        android:title="some title"
        android:summary="some summary"
        />

    <EditTextPreference
        android:key="app_id_preference3"
        android:title="some title"
        android:summary="some summary"
        />

</PreferenceCategory>

4

1 回答 1

0

好的,这样做的正确方法是使用键调用 findPreference

于 2016-08-03T13:41:25.320 回答