2011-04-13 89 views
3

我PreferemceActivity從XML充氣:PreferenceActivity刷新問題

<PreferenceScreen 
      android:title="Appearence" 
      android:key="AppearencePref" > 
      ...... 
      <PreferenceCategory 
       android:title="Show Contact Photos"> 
       <CheckBoxPreference 
        android:title="Show Contact Photos" 
        android:summary="@string/show_contact_photos_preference" 
        android:defaultValue="true" 
        android:key="ShowContactPhotosCheckBoxPref_Appendix" /> 
      </PreferenceCategory> 
     ........ 
</PreferenceScreen> 

....... 

<PreferenceScreen 
      android:title="Contact Options" 
      android:key="ContactOtionsPref"> 
      <PreferenceCategory 
       android:title="Show Contact Photos"> 
       <CheckBoxPreference 
        android:title="Show Contact Photos" 
        android:defaultValue="true" 
        android:key="ShowContactPhotosCheckBoxPref" /> 
      </PreferenceCategory> 
......    
</PreferenceScreen> 

一首(複選框)其他複選框的變換狀態:

if("ShowContactPhotosCheckBoxPref_Appendix".equals(key)){ 
      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); 
      boolean isChecked = prefs.getBoolean("ShowContactPhotosCheckBoxPref_Appendix", false); 
      Editor editor = PreferenceManager.getDefaultSharedPreferences(mContext).edit(); 
      editor.putBoolean("ShowContactPhotosCheckBoxPref", isChecked); 
      editor.commit();    
     } 

但是,當我去ShowContactPhotosCheckBoxPref篩選仍保持以前的偏好值... 因此,如果我點擊ShowContactPhotosCheckBoxPref_Appendix - 他的狀態現在是未選中狀態,然後用ShowContactPhotosCheckBoxPref進入屏幕 - 他的狀態仍然檢查,但SharedPreferences中的值爲false ...

如何告訴PreferenceActivity刷新其價值?

回答

1

解決方案是在ApiDemos AdvancedPreferences.java

private CheckBoxPreference mCheckBoxPreference; 

mCheckBoxPreference = (CheckBoxPreference)getPreferenceScreen().findPreference(
     KEY_ADVANCED_CHECKBOX_PREFERENCE); 

if (mCheckBoxPreference != null) { 
    mCheckBoxPreference.setChecked(!mCheckBoxPreference.isChecked()); 
} 
0

您沒有更改代碼中的值。它應該是:

editor.putBoolean("ShowContactPhotosCheckBoxPref", !isChecked); 

通知的!

+0

沒有,我改變數值。 ShowContactPhotosCheckBoxPref的值必須與ShowContactPhotosCheckBoxPref_Appendix的值相同 – vsvydenko 2011-04-13 09:12:21

+0

對不起,我的錯誤。我沒有正確閱讀你的帖子。 – rajath 2011-04-13 09:14:46

+0

因此,如果我點擊ShowContactPhotosCheckBoxPref_Appendix - 他的狀態現在是未選中狀態,然後使用ShowContactPhotosCheckBoxPref進行屏幕顯示 - 他的狀態仍然被選中,但SharedPreferences中的值爲false ... – vsvydenko 2011-04-13 09:17:05