更新后,从(分)preferenceScreen返回现有preference项目在preferenceActivity项目、preferenceScreen、preference、preference

2023-09-06 03:07:27 作者:那个人用菊花

我有一个preferenceActivity一堆的(副)preferenceScreens。重新每一个这样的(副)preferenceScreen presents一个帐户,并拥有账户的用户名作为它的标题。

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.

PreferenceScreen root = mgr.createPreferenceScreen(this);
for (MyAccountClass account : myAccounts) {
    final PreferenceScreen accScreen = mgr.createPreferenceScreen(this);

    accScreen.setTitle(account.getUsername());

    // add Preferences to the accScreen
    // (for instance a "change username"-preference)
    ...

    root.add(accScreen);
}

当用户输入子preferenceScreen,和编辑帐户的用户名,我想外preferenceScreen更新它的preferenceScreen标题有问题的帐户。

As the user enters sub-PreferenceScreen, and edits the account user-name, I want the outer PreferenceScreen to update it's PreferenceScreen-title for the account in question.

我试图添加...

usernamePref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        accScreen.setTitle(newValue.toString());
        return true;
    }
});

...但accScreen.setTitle似乎并没有采取外preferenceScreen效果。我已经注意到,在调用 onContentChanged(); 实际上使得它的工作,但我知道这可能不是这样做的preferred方式

...but the accScreen.setTitle does not seem to take effect on the outer PreferenceScreen. I've note that calling onContentChanged(); actually makes it work, but I realize that this is probably not the preferred way of doing it.

我怀疑我应该叫 postInvalidate()上的一些观点的地方,但我实在想不出什么看法和什么时候做。

I suspect I should call postInvalidate() on some view somewhere, but I really can't figure out on what view and when to do it.

http://stackoverflow.com/questions/2396153/$p$pferencescreen-androidsummary-update可能是experiening同样的问题我。

http://stackoverflow.com/questions/2396153/preferencescreen-androidsummary-update may be experiening the same problem as me.

任何帮助AP preciated。

Any help appreciated.

推荐答案

我找到了解决这个。我有这样一个层次,每一项都是一个preferenceScreen:

I found a solution to this. I have a hierarchy like this, each of these is a PreferenceScreen:

main settings
  -> users list
    -> user1 settings
    -> user2 settings
    ...

在用户列表中,子屏幕的标题是依赖于用户设置。现在,当我创建的用户列表,我保存在我的preferenceActivity列表​​适配器给一个变量。

In the users list, title of the sub-screen is dependent on the user settings. Now when I create the user list, I store the list adapter to a variable in my PreferenceActivity.

 PreferenceScreen usersListScreen = ...
 userScreenListAdapter = (BaseAdapter)usersListScreen.getRootAdapter();

现在,当userX-设置进行编辑,我在usersListScreen设置标题和调用后:

Now when userX-settings are edited, I set the titles in the usersListScreen and after that call:

userScreenListAdapter.notifyDataSetChanged();

该更新列表UI和变化是显而易见的。

which updates the list UI and the changes are visible.