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

  • 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;
}
主站蜘蛛池模板: 安达市| 南雄市| 广汉市| 临海市| 绥棱县| 榆中县| 潼南县| 大姚县| 枝江市| 宣恩县| 平顶山市| 铜陵市| 乡宁县| 昭苏县| 南雄市| 高阳县| 崇左市| 南宫市| 措美县| 江都市| 汉阴县| 西畴县| 新疆| 会宁县| 蓬安县| 中宁县| 兴文县| 库尔勒市| 彰武县| 邹平县| 乌兰县| 阿拉善左旗| 康乐县| 新龙县| 高雄市| 文安县| 上栗县| 长宁区| 高平市| 宁陕县| 亳州市|