我有 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;
}