File di preferenze condivisi singoli o multipli
Se hai più Shared Preference
file, ecco un buon modo per mostrarli tutti, ma puoi anche passare anche 1 nome file.
loadSharedPrefs("pref_name");
loadSharedPrefs("shared_pref1", "shared_pref2", "shared_pref3");
Scegli una delle seguenti opzioni in base alle tue esigenze ...
Valori di tipo singolo
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", "----------------------------------");
}
Valori di più tipi
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", "------------------------------------");
}
}
Uscita Logcat
I miei Shared Preference
valori sono tutti String
, ma questo è l'output utilizzando uno dei 2 metodi sopra ...
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﹕ ------------------------------------