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

  • Android Wear Projects
  • Ashok Kumar S
  • 126字
  • 2021-07-15 17:17:57

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;
}
主站蜘蛛池模板: 澎湖县| 无为县| 黄山市| 渭源县| 新和县| 秦皇岛市| 天门市| 宣恩县| 原阳县| 万宁市| 巩义市| 凤城市| 新疆| 拉萨市| 朝阳县| 清河县| 宣威市| 正镶白旗| 左权县| 柯坪县| 嵊州市| 车致| 德昌县| 文安县| 灌阳县| 庆安县| 特克斯县| 三门峡市| 汽车| 开封市| 繁峙县| 上饶市| 昌乐县| 内黄县| 南投县| 孟州市| 腾冲县| 太保市| 岢岚县| 阜新市| 宣武区|