Android learning column - practical project cool weather creates databases and tables

Series articles

Tip: go to the Android learning column and watch more!
Click me directly – > Android learning column
In this project, pay attention to the modification of the package name prefix and change it to your own. My package name is:
com.example.coolweather, otherwise an error will be reported.

preface

Refer to Mr. Guo Lin's "book on the first line of code", because the original code cannot run on android studio 3.0, my modified code is suitable for the latest version of android studio.
I strongly recommend that you read this book!
First, we create the database and tables.

1. Achieve results

Create databases and tables, and upload files to gitee warehouse. (not git in Mr. Guo's book)

2. Project structure

Create the following packages under the com.coolweather.android package

  1. The db package is used to store the code related to the database model
  2. The gson package is used to store the code related to the gson model
  3. The service package is used to store service related code
  4. The util package is used to store tool related code.
db
gson
service
util


Load libraries and dependencies

Use LitePal to manage the cool weather database
Edit the app/build.gradle file

    //Newly added
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'junit:junit:4.12'
    implementation 'org.litepal.android:core:1.4.1'
    implementation 'com.squareup.okhttp3:okhttp:3.4.1'
    implementation 'com.google.code.gson:gson:2.7'
    implementation 'com.github.bumptech.glide:glide:3.7.0'

db package

Every entity class in LitePal must inherit from the DataSupport class
The of the entity class declares the required fields and generates the corresponding getter and setter methods

3.1 create a new Province class under db package

  1. id is a field that should be present in every entity class
  2. provinceName records the name of the province
  3. provinceCode records the code of the province
package com.example.coolweather.db;

import org.litepal.crud.DataSupport;

public class Province extends DataSupport {
    private int id;
    private String provinceName;
    private int provinceCode;

    public int getId() {
        return id;
    }

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

    public String getProvinceName() {
        return provinceName;
    }

    public void setProvinceName(String provinceName) {
        this.provinceName = provinceName;
    }

    public int getProvinceCode() {
        return provinceCode;
    }

    public void setProvinceCode(int provinceCode) {
        this.provinceCode = provinceCode;
    }
}

3.2 create a new City class under db package

  1. cityName records the name of the city
  2. cityCode records the city code
  3. provinceId records the id value of the province to which the current city belongs
package com.example.coolweather.db;

import org.litepal.crud.DataSupport;

public class City extends DataSupport {
    private int id;
    private String cityName;
    private int cityCode;
    private int provinceId;

    public int getId() {
        return id;
    }

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

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public int getCityCode() {
        return cityCode;
    }

    public void setCityCode(int cityCode) {
        this.cityCode = cityCode;
    }

    public int getProvinceId() {
        return provinceId;
    }

    public void setProvinceId(int provinceId) {
        this.provinceId = provinceId;
    }
}

3.3 create a new Country class under db package

  1. countyName records the name of the county
  2. weatherId record the weather id corresponding to the county
  3. cityId records the id value of the city to which the current county belongs.
package com.example.coolweather.db;

import org.litepal.crud.DataSupport;

public class County extends DataSupport {
    private int id;
    private String countyName;
    private String weatherId;
    private int cityId;

    public int getId() {
        return id;
    }

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

    public String getCountyName() {
        return countyName;
    }

    public void setCountyName(String countyName) {
        this.countyName = countyName;
    }

    public String getWeatherId() {
        return weatherId;
    }

    public void setWeatherId(String weatherId) {
        this.weatherId = weatherId;
    }

    public int getCityId() {
        return cityId;
    }

    public void setCityId(int cityId) {
        this.cityId = cityId;
    }
}

4. Configure litepal

4.1 configuring litepal

Right click app/src/main Directory → New → Directory to create an assets Directory, and then create a New Directory under the assets Directory
Create a litepal.xml file, and then edit the litepal.xml file.

Specify the database name as cool_weather,
The database version is specified as 1,
Add the three entity classes of Province, City and County to the mapping list.

<litepal>
    <dbname value="cool_weather" />
    <version value="1" />
    <list>
        <mapping class="com.example.coolweather.db.Province" />
        <mapping class="com.example.coolweather.db.City" />
        <mapping class="com.example.coolweather.db.County" />
    </list>
</litepal>

4.2 configuring LitePalApplication

Configure LitePalApplication and modify AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.coolweather">

    <application
        android:name="org.litepal.LitePalApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.CoolWeather"
        >
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Appendix. References

The first line of code 14.3 create database and table

Download resources

The content has been uploaded to the gitee repository

https://gitee.com/miao-zehao/cool-weather/tree/master/


Android learns to create databases and examples in the actual combat project cool weather (1)

summary

If you like, give me a 👍, Pay attention! Continue to share with you the problems encountered in the process of typing code!

Tags: Java Android Android Studio

Posted on Wed, 24 Nov 2021 18:30:18 -0500 by 3dron