views:

40

answers:

1

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;
}
A: 

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);
Pentium10
That line is error free in Eclipse is but that exact line throws the cast error. java.lang.ClassCastException: android.preference.PreferenceScreen The first pref is a PreferenceScreen that loads all songs on sdcard using intent. The second is a simple ListPreference which just returns 1 of three states default,music, or mute.
Tim Wayne
Post your prefs screen layout.
Pentium10
Ok, I did. I could add some preference keys to the SharedPrefs and do this a different way. It's just that the code was working fine and the last thing I did was set the summary in onSharedPreferencesChanged. I also need to update the setEntryValues there as well.
Tim Wayne