I have the following Preference class:
public class AppPreferencesActivity extends PreferenceActivity {
private SharedPreferences appPrefs;
private SharedPreferences.Editor prefEditor;
private Mediator mediator;
private SharedPreferences.OnSharedPreferenceChangeListener listener;
private User user;
@SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//---load the preferences from an XML file---
addPreferencesFromResource(R.xml.preferences);
this.appPrefs = PreferenceManager.getDefaultSharedPreferences(this);
this.mediator = (Mediator) getApplication();
this.prefEditor = appPrefs.edit();
this.user = mediator.getUser();
setPreferences();
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
// Implementation
updatePreference(key);
Toast.makeText(AppPreferencesActivity.this,
"Information updated",
Toast.LENGTH_SHORT).show();
}
};
appPrefs.registerOnSharedPreferenceChangeListener(listener);
}
private void setPreferences(){
prefEditor.putString("nameEdit", user.getName() + " "+user.getLastName());
prefEditor.putString("streetEdit", user.getStreetName());
prefEditor.putString("streetNumberEdit", ""+user.getStreetNr());
prefEditor.putString("emailEdit", user.getEmail());
prefEditor.commit();
preferenceAddSummary();
}
public boolean checkEmailStatus(){
return appPrefs.contains("emailEdit");
}
@SuppressWarnings("deprecation")
private void preferenceAddSummary() {
for (Entrykey : appPrefs.getAll().entrySet()) {
findPreference(key.getKey()).setSummary(appPrefs.getString(key.getKey(), "Not yet entered"));
// this is where im getting the error
}
However when i run this i get the following
Cannot cast Java.lang.boolean to Java.lang.String
I get this error at the loop im calling to setSumary can anyone tell me why this is happening?
UPDATE
Here is a print of my stacktrace:
.
stackoverflow.comm
Im a novice myself but just solved this issue.
ReplyDeletePreferences.get() returns your Map, but BOTH the key AND value get returned as a STRING, NOT a boolean.
I just did
boolean value=false;
if(pref.get(key).equals("true")) {
value=true;
}