- 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.
推薦閱讀
- Vue.js 3.x快速入門
- ClickHouse性能之巔:從架構設計解讀性能之謎
- 看透JavaScript:原理、方法與實踐
- Java深入解析:透析Java本質的36個話題
- C#程序設計
- 批調度與網絡問題的組合算法
- 從0到1:HTML5 Canvas動畫開發
- SwiftUI極簡開發
- 數據分析與挖掘算法:Python實戰
- 大數據時代的企業升級之道(全3冊)
- Practical Predictive Analytics
- Julia High Performance(Second Edition)
- Arduino電子設計實戰指南:零基礎篇
- Python編程入門(第3版)
- Keil Cx51 V7.0單片機高級語言編程與μVision2應用實踐