官术网_书友最值得收藏!

Fetching all the saved notes from SharedPreference

Fetching all the notes for this List type method needs a Context and then the SharedPreference reference using the PreferenceManager class. Then, we will create an instance of a List of notes for adding the notes after fetching it. We use the Map type for looping through the saved data inside SharedPreference. We will add all the data to the list inside the loop; it returns the noteList: 

public static List<Note> getAllNotes(Context context) {
    SharedPreferences sharedPreferences = 
    PreferenceManager.getDefaultSharedPreferences(context);
    List<Note> noteList = new ArrayList<>();
    Map<String, ?> key = sharedPreferences.getAll();
    for (Map.Entry<String, ?> entry : key.entrySet()) {
        String savedValue = (String) entry.getValue();

        if (savedValue != null) {
            Note note = new Note(entry.getKey(), savedValue);
            noteList.add(note);
        }
    }
    return noteList;
}
主站蜘蛛池模板: 永清县| 宾川县| 瑞金市| 温州市| 永年县| 萝北县| 锦屏县| 米易县| 金山区| 眉山市| 罗山县| 施秉县| 新乡县| 鸡西市| 博乐市| 准格尔旗| 垣曲县| 水富县| 运城市| 新安县| 孝义市| 浠水县| 佛坪县| 长兴县| 米脂县| 大关县| 美姑县| 德惠市| 屏南县| 汉源县| 拉孜县| 湾仔区| 任丘市| 江北区| 五大连池市| 龙南县| 元谋县| 宁津县| 香格里拉县| 渭南市| 桐城市|