Archivos de preferencias compartidos únicos o múltiples
Si tiene varios Shared Preference
archivos, esta es una buena manera de mostrarlos todos, pero también puede pasar 1 nombre de archivo.
loadSharedPrefs("pref_name");
loadSharedPrefs("shared_pref1", "shared_pref2", "shared_pref3");
Elija uno de los siguientes para satisfacer sus necesidades ...
Valores de tipo único
public void loadSharedPrefs(String ... prefs) {
// Logging messages left in to view Shared Preferences. I filter out all logs except for ERROR; hence why I am printing error messages.
Log.i("Loading Shared Prefs", "-----------------------------------");
Log.i("----------------", "---------------------------------------");
for (String pref_name: prefs) {
SharedPreferences preference = getSharedPreferences(pref_name, MODE_PRIVATE);
for (String key : preference.getAll().keySet()) {
Log.i(String.format("Shared Preference : %s - %s", pref_name, key),
preference.getString(key, "error!"));
}
Log.i("----------------", "---------------------------------------");
}
Log.i("Finished Shared Prefs", "----------------------------------");
}
Valores de varios tipos
public void loadSharedPrefs(String ... prefs) {
// Define default return values. These should not display, but are needed
final String STRING_ERROR = "error!";
final Integer INT_ERROR = -1;
// ...
final Set<String> SET_ERROR = new HashSet<>(1);
// Add an item to the set
SET_ERROR.add("Set Error!");
// Loop through the Shared Prefs
Log.i("Loading Shared Prefs", "-----------------------------------");
Log.i("------------------", "-------------------------------------");
for (String pref_name: prefs) {
SharedPreferences preference = getSharedPreferences(pref_name, MODE_PRIVATE);
Map<String, ?> prefMap = preference.getAll();
Object prefObj;
Object prefValue = null;
for (String key : prefMap.keySet()) {
prefObj = prefMap.get(key);
if (prefObj instanceof String) prefValue = preference.getString(key, STRING_ERROR);
if (prefObj instanceof Integer) prefValue = preference.getInt(key, INT_ERROR);
// ...
if (prefObj instanceof Set) prefValue = preference.getStringSet(key, SET_ERROR);
Log.i(String.format("Shared Preference : %s - %s", pref_name, key),
String.valueOf(prefValue));
}
Log.i("------------------", "-------------------------------------");
}
Log.i("Loaded Shared Prefs", "------------------------------------");
}
}
Salida Logcat
Mis Shared Preference
valores son todos String
, pero esta es la salida usando cualquiera de los 2 métodos anteriores ...
I/Loading Shared Prefs﹕ -----------------------------------
I/------------------﹕ -------------------------------------
I/Shared Preference : FAVORITE - 135397﹕ Jurassic World
I/Shared Preference : FAVORITE - 87101﹕ Terminator Genisys
I/Shared Preference : FAVORITE - 177677﹕ Mission: Impossible – Rogue Nation
I/------------------﹕ -------------------------------------
I/Shared Preference : WATCHED - 177677﹕ Mission: Impossible – Rogue Nation
I/Shared Preference : WATCHED - 157336﹕ Interstellar
I/Shared Preference : WATCHED - 135397﹕ Jurassic World
I/Shared Preference : WATCHED - 87101﹕ Terminator Genisys
I/------------------﹕ -------------------------------------
I/Shared Preference : WILL_WATCH - 211672﹕ Minions
I/Shared Preference : WILL_WATCH - 102899﹕ Ant-Man
I/------------------﹕ -------------------------------------
I/Loaded Shared Prefs﹕ ------------------------------------