Satu atau Beberapa File Preferensi Bersama
Jika Anda memiliki banyak Shared Preference
file, berikut adalah cara yang baik untuk menampilkan semuanya, tetapi Anda juga dapat memasukkan 1 nama file juga.
loadSharedPrefs("pref_name");
loadSharedPrefs("shared_pref1", "shared_pref2", "shared_pref3");
Pilih salah satu dari berikut ini untuk menyesuaikan dengan kebutuhan Anda ...
Nilai Tipe Tunggal
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", "----------------------------------");
}
Nilai Beberapa Jenis
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", "------------------------------------");
}
}
Keluaran Logcat
Shared Preference
Nilai saya semuanya String
, tetapi ini adalah output menggunakan salah satu dari 2 metode di atas ...
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﹕ ------------------------------------