With android I want to have 2 preferences with the same key
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.addPrefer开发者_如何学Pythonence(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 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
}
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);
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.
精彩评论