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

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. 

主站蜘蛛池模板: 卢湾区| 密山市| 乌什县| 逊克县| 霍邱县| 富蕴县| 彭州市| 宝丰县| 谷城县| 德安县| 哈巴河县| 陇南市| 凭祥市| 景东| 绥宁县| 亳州市| 晋宁县| 金湖县| 江安县| 开原市| 鄂尔多斯市| 古交市| 莲花县| 专栏| 新邵县| 沽源县| 安丘市| 宁陕县| 新平| 黄骅市| 高碑店市| 安达市| 扶余县| 禹州市| 筠连县| 通辽市| 鹰潭市| 孟州市| 平原县| 孟津县| 胶南市|