- Android Wear Projects
- Ashok Kumar S
- 228字
- 2021-07-15 17:17:57
Creating the SharedPreference utility
The SharedPreference utility class acts as an interface between the XML key value pair to read and write the data. We will create three methods for saving notes, fetching all the notes, and removing one particular note:
- saveNote(..);
- getAllNotes(..);
- removeNote(..);
Inside the util package, let's create two Java classes called Constants.java and SharedPreferencesUtils.java.
Inside Constants.java, let's add the following constants:
public static final String NOTE_ID = "NOTE_ID"; public static final String NOTE_TITLE = "NOTE_TITLE"; public static final String NOTE_LIST = "NOTE_LIST"; public static final String ACTION = "ACTION"; public static final String ITEM_POSITION = "ITEM_POSITION"; public static final int ACTION_ADD = 9001; public static final int ACTION_DELETE = 9002; public static final int ACTION_SYNC = 9003;
The complete class looks as follows:
package com.ashok.packt.wear_note_1.utils; /** * Created by ashok.kumar on 15/02/17. */ public class Constants { public static final String NOTE_ID = "NOTE_ID"; public static final String NOTE_TITLE = "NOTE_TITLE"; public static final String NOTE_LIST = "NOTE_LIST"; public static final String ACTION = "ACTION"; public static final String ITEM_POSITION = "ITEM_POSITION"; public static final int ACTION_ADD = 9001; public static final int ACTION_DELETE = 9002; public static final int ACTION_SYNC = 9003; }
Now, it's the time to work on the SharedPreferencesUtils.java class. In this class, we will create the methods to save data, retrieve data, and delete data.
推薦閱讀
- 數(shù)據(jù)庫程序員面試筆試真題與解析
- Mastering ServiceStack
- 程序員數(shù)學:用Python學透線性代數(shù)和微積分
- Python網(wǎng)絡爬蟲從入門到實踐(第2版)
- C#程序設計基礎:教程、實驗、習題
- Python機器學習算法: 原理、實現(xiàn)與案例
- Android開發(fā)三劍客:UML、模式與測試
- Image Processing with ImageJ
- Mastering Elixir
- Windows Phone 8 Game Development
- PostgreSQL Developer's Guide
- 一覽眾山小:ASP.NET Web開發(fā)修行實錄
- Eclipse開發(fā)(學習筆記)
- Scratch超人漫游記:創(chuàng)意程序設計:STEAM創(chuàng)新教育指南
- MATLAB/Simulink建模與仿真