Fichiers de préférences partagés uniques ou multiples
Si vous avez plusieurs Shared Preference
fichiers, alors voici un bon moyen de les afficher tous, mais vous pouvez également passer un nom de fichier.
loadSharedPrefs("pref_name");
loadSharedPrefs("shared_pref1", "shared_pref2", "shared_pref3");
Choisissez l'une des options suivantes en fonction de vos besoins ...
Valeurs de type unique
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", "----------------------------------");
}
Valeurs de type multiple
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", "------------------------------------");
}
}
Sortie Logcat
Mes Shared Preference
valeurs sont toutes String
, mais c'est la sortie en utilisant l'une des 2 méthodes ci-dessus ...
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﹕ ------------------------------------