2014-02-18 59 views
0

我已經設置了默認偏好處理,在Android中設置W片段說,那很好,但我有一個未決問題:Android的 - 默認的喜好問題,當重新顯示

當我啓動應用程序,並轉到如果我修改了一個鍵,當前設置的defaultValue被正確設置並顯示...

當我退出應用程序並重新啓動它時,即使defaultValue在列表中更改,當前設置的顯示仍然是在初始默認值

這裏是Preferences.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"); 

     }   
} 

}

和我的preferences.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> 

回答

0

嘗試改變:

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

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