对于 android,我希望使用相同的键有 2 个首选项
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我无法使用
findPreference
使其正常工作。但是,
PreferenceGroup
有方法可以迭代内部的所有首选项:getPreferenceCount()
getPreference(int index)
所以我简单地编写了一个方法,它会迭代遍历组中的所有首选项,并仅返回具有给定键的首选项:
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:
你可以,但你做错了。您仍然在主屏幕上调用 find。
您需要在检索到的首选项屏幕上显式调用,因为您想进一步剖析它。
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.
您将一个
Object
传递给 setSummary(),它需要一个int
。您可能会收到错误,因为您将其转换为int
。只是猜测这里似乎缺少一些代码。you're passing an
Object
to setSummary(), which expects anint
. You're probably getting the error because you cast it to be anint
. Just guessing here, there seems to be missing some code.