对于 android,我希望使用相同的键有 2 个首选项

发布于 2024-09-10 03:52:20 字数 2479 浏览 3 评论 0原文

我有 2 个首选项 - 我想存储在同一键中的歌曲标题和音频状态(静音或音量)。这适用于点击,我遇到的唯一问题是重置 onSharedPreferencesChanged 上的摘要,我遇到了错误。

PreferenceScreen musicPrefScreen = (PreferenceScreen)getPreferenceScreen
        ().findPreference("theme" + Team_ID); 
musicPrefScreen.setSummary(mPreferences.getString("theme" + 
        Team_ID, "CantGetTeam")); 
ListPreference audiostatePref = (ListPreference)getPreferenceScreen()
        .findPreference("theme" + Team_ID);
audiostatePref.setSummary(AudioState); 
audiostatePref.setEntryValues(audiostates_values);

因为第二次调用 findPreference 返回第一个 Pref。文档说您可以在第一个上调用 findPreference 来获取第二个,但它们是不同类型的首选项,我收到强制转换错误。是否有另一种方法可以找到 ListPreference,以便我可以重置列表上的条目值?

编辑: 这是布局

public PreferenceScreen createPreferenceHierarchy() { 
    PreferenceScreen prefScreenRoot = getPreferenceManager().createPreferenceScreen(this);

    PreferenceCategory TeamCategory = new PreferenceCategory(this); 
    TeamCategory.setTitle("Team " + Team_ID + " Settings"); 
    prefScreenRoot.addPreference(TeamCategory);
        // set team name
        EditTextPreference teamnamePref = new EditTextPreference(this);  
        teamnamePref.setTitle("Team Name"); 
        teamnamePref.setKey( "team" + Team_ID );
        teamnamePref.setSummary(TheTeamName); 
        teamnamePref.setDialogTitle("Enter Name For Team " + Team_ID);
        teamnamePref.setDefaultValue(TheTeamName);
        teamnamePref.getEditText().setSingleLine(true);
        TeamCategory.addPreference(teamnamePref);   

        // select theme music
        PreferenceScreen musicPrefScreen = getPreferenceManager().createPreferenceScreen(this); 
        Intent musicIntent = new Intent(this, MusicDroid.class); 
        musicIntent.putExtra( "team_id", Team_ID);  
        musicPrefScreen.setIntent(musicIntent); 
        musicPrefScreen.setKey( "theme" + Team_ID );
        musicPrefScreen.setTitle("Theme Music"); 
        musicPrefScreen.setSummary(TheThemeName); 
        TeamCategory.addPreference(musicPrefScreen);  

        // select audio state
        ListPreference audiostatePref = new ListPreference(this);
        audiostatePref.setTitle("Audio State"); 
        audiostatePref.setKey( "theme" + Team_ID );
        audiostatePref.setSummary(AudioState); // 
        audiostatePref.setEntries(audiostates);
        audiostatePref.setEntryValues(audiostates_values);
        TeamCategory.addPreference(audiostatePref); 

    return prefScreenRoot;
}

I have 2 prefs - a song title and the audio state(mute or volume) that I want to store in the same key. This works for the clicks and the only problem I have is resetting the summary on onSharedPreferencesChanged I get errors with this.

PreferenceScreen musicPrefScreen = (PreferenceScreen)getPreferenceScreen
        ().findPreference("theme" + Team_ID); 
musicPrefScreen.setSummary(mPreferences.getString("theme" + 
        Team_ID, "CantGetTeam")); 
ListPreference audiostatePref = (ListPreference)getPreferenceScreen()
        .findPreference("theme" + Team_ID);
audiostatePref.setSummary(AudioState); 
audiostatePref.setEntryValues(audiostates_values);

because the second call to findPreference returns the first Pref. The docs say you can call findPreference on the first to get the second but they are different types of prefs and i get a cast error. Is there another way to find the ListPreference so I can reset the Entry Values on the list?

EDIT:
Here is the layout

public PreferenceScreen createPreferenceHierarchy() { 
    PreferenceScreen prefScreenRoot = getPreferenceManager().createPreferenceScreen(this);

    PreferenceCategory TeamCategory = new PreferenceCategory(this); 
    TeamCategory.setTitle("Team " + Team_ID + " Settings"); 
    prefScreenRoot.addPreference(TeamCategory);
        // set team name
        EditTextPreference teamnamePref = new EditTextPreference(this);  
        teamnamePref.setTitle("Team Name"); 
        teamnamePref.setKey( "team" + Team_ID );
        teamnamePref.setSummary(TheTeamName); 
        teamnamePref.setDialogTitle("Enter Name For Team " + Team_ID);
        teamnamePref.setDefaultValue(TheTeamName);
        teamnamePref.getEditText().setSingleLine(true);
        TeamCategory.addPreference(teamnamePref);   

        // select theme music
        PreferenceScreen musicPrefScreen = getPreferenceManager().createPreferenceScreen(this); 
        Intent musicIntent = new Intent(this, MusicDroid.class); 
        musicIntent.putExtra( "team_id", Team_ID);  
        musicPrefScreen.setIntent(musicIntent); 
        musicPrefScreen.setKey( "theme" + Team_ID );
        musicPrefScreen.setTitle("Theme Music"); 
        musicPrefScreen.setSummary(TheThemeName); 
        TeamCategory.addPreference(musicPrefScreen);  

        // select audio state
        ListPreference audiostatePref = new ListPreference(this);
        audiostatePref.setTitle("Audio State"); 
        audiostatePref.setKey( "theme" + Team_ID );
        audiostatePref.setSummary(AudioState); // 
        audiostatePref.setEntries(audiostates);
        audiostatePref.setEntryValues(audiostates_values);
        TeamCategory.addPreference(audiostatePref); 

    return prefScreenRoot;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花桑 2024-09-17 03:52:20

我无法使用 findPreference 使其正常工作。

但是, PreferenceGroup 有方法可以迭代内部的所有首选项:

  • getPreferenceCount()
  • getPreference(int index)

所以我简单地编写了一个方法,它会迭代遍历组中的所有首选项,并仅返回具有给定键的首选项:

private List<Preference> findPreferences(PreferenceGroup group, String key) {
    List<Preference> prefs = new ArrayList<>();
    for (int index = 0; index < group.getPreferenceCount(); index++) {
        Preference pref = group.getPreference(index);
        if (key.equals(pref.getKey())) {
            prefs.add(pref);
        }
    }
    return prefs
}

I couldn't make it work properly by using findPreference.

However, PreferenceGroup has methods to iterate through all preferences inside:

  • getPreferenceCount()
  • getPreference(int index)

So I simply wrote a method, which iterates through all the preferences in group, and returns only the preferences with the given key:

private List<Preference> findPreferences(PreferenceGroup group, String key) {
    List<Preference> prefs = new ArrayList<>();
    for (int index = 0; index < group.getPreferenceCount(); index++) {
        Preference pref = group.getPreference(index);
        if (key.equals(pref.getKey())) {
            prefs.add(pref);
        }
    }
    return prefs
}
空城缀染半城烟沙 2024-09-17 03:52:20

文档说您可以在第一个上调用 findPreference 来获取第二个

你可以,但你做错了。您仍然在主屏幕上调用 find。

您需要在检索到的首选项屏幕上显式调用,因为您想进一步剖析它。

ListPreference audiostatePref = (ListPreference)musicPrefScreen.findPreference("theme" + Team_ID);

The docs say you can call findPreference on the first to get the second

You can but you are doing it wrong. You still call find on the main screen.

You need to call explicitly on the retrieved preference screen, as you want to anatomize that further.

ListPreference audiostatePref = (ListPreference)musicPrefScreen.findPreference("theme" + Team_ID);
予囚 2024-09-17 03:52:20

您将一个 Object 传递给 setSummary(),它需要一个 int。您可能会收到错误,因为您将其转换为 int。只是猜测这里似乎缺少一些代码。

you're passing an Object to setSummary(), which expects an int. You're probably getting the error because you cast it to be an int. Just guessing here, there seems to be missing some code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文