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

  • Android Wear Projects
  • Ashok Kumar S
  • 406字
  • 2021-07-15 17:17:57

Creating the packages

Creating packages helps in maintaining the code in a structured manner. In the Wear-note-1 application, we will create the following package structure:  

  • model
  • utils
  • adapter
  • activity

Create these packages in the Wear application and add the activity inside the activity package. The following screenshot explains how the package looks: 

  1. All the packages inside the Wear project scope. 
  2. Main Activity moved inside the activity package.

The conceptualized Wear note-taking app should save notes and should be able to delete notes. For persisting the notes, we shall use SharedPreferences, which allows all the  Create, Read, Update, and Delete (CRUD) operations in terms of key-value pairs. SharedPreferences wraps inside the Android SDK; we need not worry about installing a third-party library for this chapter. 

Let's create a POJO model called Note.java inside the model package in the Wear project scope. And let's create the following member variables inside the Note.java class:

POJO stands for Plain Old Java Object , and would be used to describe the same things as a "Normal Class." They use getters and setters to protect their member variables.
 private String notes = "";
 private String id = "";

After creating the previous member variables, we shall create a parameterized constructor as follows: 

 public Note(String id, String notes) {
        this.id = id;
        this.notes = notes;
 }

Once the constructor is completed, we need to create setters and getters for the same member variables. Android Studio helps in creating setters and getters for the requested data type using the following shortcut: 

Use the previous shortcut in the Generate... option and select Getter and Setter. Then, choose the variables that need Getter and Setter. For this note application, we only have the id and actual note. We shall choose id and note.

The complete Note.java class looks as follows:

package com.ashok.packt.wear_note_1.model;

/**
 * Created by ashok.kumar on 15/02/17.
 */

public class Note {

    private String notes = "";
    private String id = "";

    public Note(String id, String notes) {
        this.id = id;
        this.notes = notes;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

}

Now, this POJO class will take care of setting and getting the value from and to SharedPreference. 

主站蜘蛛池模板: 湘阴县| 重庆市| 若羌县| 德安县| 读书| 昌图县| 内黄县| 连州市| 渑池县| 平江县| 虞城县| 麻城市| 安仁县| 郓城县| 天全县| 红河县| 长宁县| 阿克苏市| 霍城县| 苗栗县| 永寿县| 六盘水市| 来宾市| 屏南县| 旬阳县| 富顺县| 柘荣县| 长寿区| 通海县| 洪泽县| 彝良县| 芮城县| 重庆市| 安图县| 左贡县| 抚顺县| 安陆市| 黔南| 鹤岗市| 巴青县| 黄骅市|