Site icon PCSalt

How to get all values of SharedPreferences programmatically in Android

SharedPreferences stores all values in XML file. So, if you want to iterate through all the values stored in it, you could try following code.

public static List getAllValues(Context context) {
    Map values = getPref(context).getAll();
    List prefDataList = new ArrayList();
    for (Map.Entry entry : values.entrySet()) {
        PrefData prefData = new PrefData();
        prefData.key = entry.getKey();
        prefData.value = entry.getValue().toString();
        prefDataList.add(prefData);
    }
    return prefDataList;
}


(adsbygoogle = window.adsbygoogle || []).push({});

In this code PrefData is a model class having two String variables for key and value.

public final class PrefData {
    public String key;
    public String value;
}
Exit mobile version