重新显示时出现默认首选项问题



我已经按照Android设置w片段中所述设置了默认首选项处理,它运行良好,但我有一个悬而未决的问题:

当我启动应用程序并转到首选项时,如果我修改一个键,当前设置的默认值已正确设置并显示...

当我退出应用程序并重新启动它时,即使列表中的默认值发生了变化,当前设置的显示仍然在初始默认值上

这是首选项.java

public class Preferences extends PreferenceActivity {
static final String TAG = "SWIMMER";
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragment()).commit();
  PreferenceManager.setDefaultValues(Preferences.this, R.xml.preferences, true);
 }

 public class PrefsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
     @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);  
      }
     @Override
        public void onResume() {
            super.onResume();
            // Set up a listener whenever a key changes
            getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
        }
        @Override
        public void onPause() {
            super.onPause();
            // Set up a listener whenever a key changes
            getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
        }
        @Override
        public void onDestroy() {
            super.onDestroy();
            // Set up a listener whenever a key changes
            getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
        }
        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {    
            ListPreference rlp = (ListPreference) findPreference("samplingRatePref");
            rlp.setSummary(getString(R.string.current_sampling_rate) + ": %s");                     
            ListPreference plp = (ListPreference) findPreference("samplingPointsPref");
            plp.setSummary(getString(R.string.current_sampling_points) + ": %s");                       
            ListPreference f2lp = (ListPreference) findPreference("samplingFormula2Pref");
            f2lp.setSummary(getString(R.string.current_sampling_formula2) + ": %s");
            ListPreference f4lp = (ListPreference) findPreference("samplingFormula4Pref");
            f4lp.setSummary(getString(R.string.current_sampling_formula4) + ": %s");                        
            ListPreference f8lp = (ListPreference) findPreference("samplingFormula8Pref");
            f8lp.setSummary(getString(R.string.current_sampling_formula8) + ": %s");
            Log.d(TAG, "onSharedPreferenceChanged");
        }        
 }

}

和我的偏好.xml :

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

<PreferenceCategory android:title="Preferences" >
    <ListPreference
        android:defaultValue="@string/initial_sampling_rate"
        android:entries="@array/listArray"
        android:entryValues="@array/listValues"
        android:key="samplingRatePref"
        android:summary="currently set to 10 ms"
        android:title="Sampling Rate" />
    <ListPreference
        android:defaultValue="@string/initial_sampling_points"
        android:entries="@array/pointsListArray"
        android:entryValues="@array/pointsListValues"
        android:key="samplingPointsPref"
        android:summary="currently set to 2"
        android:title="Sampling Points" />
    <ListPreference
        android:defaultValue="@string/initial_sampling_formula2"
        android:entries="@array/formula2ListArray"
        android:entryValues="@array/formula2ListValues"
        android:key="samplingFormula2Pref"
        android:summary="currently set to linear"
        android:title="Sampling 2 points formula" />
    <ListPreference
        android:defaultValue="@string/initial_sampling_formula4"
        android:entries="@array/formula4ListArray"
        android:entryValues="@array/formula4ListValues"
        android:key="samplingFormula4Pref"
        android:summary="currently set to cubic"
        android:title="Sampling 4 points formula" />
    <ListPreference
        android:defaultValue="@string/initial_sampling_formula8"
        android:entries="@array/formula8ListArray"
        android:entryValues="@array/formula8ListValues"
        android:key="samplingFormula8Pref"
        android:summary="currently set to linear"
        android:title="Sampling 8 points formula" />
</PreferenceCategory>

尝试更改:

PreferenceManager.setDefaultValues(Preferences.this, R.xml.preferences, true);

PreferenceManager.setDefaultValues(Preferences.this, R.xml.preferences, false);

相关内容

  • 没有找到相关文章

最新更新