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

  • 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;
}
主站蜘蛛池模板: 陆丰市| 玉田县| 闸北区| 博爱县| 富顺县| 女性| 鸡泽县| 玉树县| 石景山区| 乌拉特前旗| 湖南省| 建阳市| 柳林县| 犍为县| 调兵山市| 抚顺市| 衡阳县| 淮阳县| 黄大仙区| 旌德县| 宣恩县| 阳泉市| 沙雅县| 象山县| 中牟县| 玛多县| 乌拉特后旗| 新闻| 彩票| 荔波县| 胶南市| 江都市| 迁安市| 贺州市| 望江县| 朝阳县| 扎鲁特旗| 监利县| 收藏| 鞍山市| 洛阳市|