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

  • 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. 

主站蜘蛛池模板: 东阿县| 普兰县| 郯城县| 漯河市| 汤阴县| 珲春市| 永昌县| 合水县| 准格尔旗| 晋宁县| 大足县| 淮北市| 昌吉市| 绥棱县| 鄂伦春自治旗| 龙岩市| 伊金霍洛旗| 波密县| 咸宁市| 宣城市| 财经| 潍坊市| 乌鲁木齐市| 缙云县| 清水河县| 柳州市| 隆子县| 政和县| 右玉县| 石渠县| 庆云县| 阿尔山市| 连山| 安新县| 冷水江市| 彰武县| 彭山县| 砚山县| 石渠县| 陇南市| 民勤县|