Update existing Preference-item in a PreferenceActivity upon returning from a (sub)PreferenceScreen

前端 未结 8 1784
清歌不尽
清歌不尽 2020-12-16 19:36

I have a PreferenceActivity with a bunch of (Sub)PreferenceScreens. Each such (Sub)PreferenceScreen represents an account and has the account-username as its title.

相关标签:
8条回答
  • 2020-12-16 20:39

    Experiencing this same problem, but onContentChanged() isn't working for me. My problem is with PreferenceScreens that are more than one level deep from the root.

    To follow your example, if you first created an "Accounts" PreferenceScreen, and then added each of your individual account PreferenceScreen objects under that. Like this:

    Root Screen
      -> "Accounts" screen
        -> "foo@example.com" screen
          -> edit username
          -> edit password
          -> etc...
        -> "bar@example.com" screen
        -> "baz@example.com" screen
        -> etc...
    

    If a user edited their username and clicked save, calling PreferenceActivity.onContentChanged() seems to only affect direct descendants of the root PreferenceScreen. The third-generation screens' titles and summaries do not get redrawn, still reflecting old values.

    Looking through the code for onContentChanged(), it looks like it just re-bind()s the root Screen to the ListActivity's ListView, although I don't think subsequent PreferenceScreens are ever bound to a ListView (are they?), so we can't manually re-bind anything...

    The only workaround I can think of would be to create the sub-menus as isolated PreferenceActivitys instead of PreferenceScreens, so we can intentionally call onContentChanged() on our direct ancestor. But that's even more of a kludge than the current workaround. Any ideas?

    0 讨论(0)
  • 2020-12-16 20:41

    Source

    // Import required classes (Win: CTRL+SHIFT+O & Mac: CMD+SHIFT+O)
    public class YourCustomPreference extends PreferenceActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preferences);
        }
    
        // some logic goes above, when you want to reset value and update
        // EditTextPreference value. For convenience, I am going to wrap two
        // different task in different methods
        private void resetPreferenceValue() {
            SharedPreferences sharedPref = PreferenceManager
                    .getDefaultSharedPreferences(this.getApplicationContext());
            // Get preference in editor mode
            SharedPreferences.Editor prefEditor = sharedPref.edit();
            // set your default value here (could be empty as well)
            prefEditor.putString("your_edit_text_pref_key", "DEFAULT-VALUE");
            prefEditor.commit(); // finally save changes
            // Now we have updated shared preference value, but in activity it
            // still hold the old value
            this.resetElementValue();
        }
    
        private void resetElementValue() {
            // First get reference to edit-text view elements
            EditTextPreference myPrefText = (EditTextPreference) super
                    .findPreference("your_edit_text_pref_key");
            // Now, manually update it's value to default/empty
            myPrefText.setText("DEFAULT-VALUE");
            // Now, if you click on the item, you'll see the value you've just
            // set here
        }
    }
    
    0 讨论(0)
提交回复
热议问题