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

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. 

主站蜘蛛池模板: 平南县| 青阳县| 辽宁省| 屏边| 临潭县| 桂平市| 南京市| 兴安盟| 阿合奇县| 阳曲县| 浦江县| 多伦县| 西昌市| 山西省| 兴安县| 芜湖县| 蓬溪县| 台中市| 吉安县| 高雄县| 靖宇县| 伊川县| 龙里县| 万年县| 喀喇| 左贡县| 游戏| 白沙| 乌鲁木齐市| 曲阜市| 基隆市| 牟定县| 武汉市| 崇义县| 平塘县| 锡林浩特市| 大石桥市| 石城县| 新巴尔虎左旗| 福清市| 道孚县|