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

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;
}
主站蜘蛛池模板: 紫阳县| 瓦房店市| 英山县| 正镶白旗| 元江| 万安县| 阿拉善左旗| 岢岚县| 西昌市| 巴彦淖尔市| 彩票| 阜阳市| 蒲城县| 阿鲁科尔沁旗| 林芝县| 葫芦岛市| 东阳市| 德庆县| 巴楚县| 新蔡县| 历史| 满洲里市| 蒙城县| 嵊州市| 垦利县| 大竹县| 石柱| 宽甸| 保亭| 阿克苏市| 蓝田县| 习水县| 宁波市| 黑龙江省| 兴城市| 山丹县| 壤塘县| 诏安县| 澜沧| 大田县| 武安市|