Android配置LitePal
Posted Demon121884344
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android配置LitePal相关的知识,希望对你有一定的参考价值。
1、引入库
项目地址:https://github.com/guolindev/LitePal
在 build.gradle 中如下配置
dependencies {
implementation 'org.litepal.guolindev:core:3.2.3'
}
2、配置litepal.xml
在main文件下新建文件夹assets,再在其中创建litepal.xml文件
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<!--
Define the database name of your application.
By default each database name should be end with .db.
If you didn't name your database end with .db,
LitePal would plus the suffix automatically for you.
For example:
<dbname value="demo" />
-->
<dbname value="demo" />
<!--
Define the version of your database. Each time you want
to upgrade your database, the version tag would helps.
Modify the models you defined in the mapping tag, and just
make the version value plus one, the upgrade of database
will be processed automatically without concern.
For example:
<version value="1" />
-->
<version value="1" />
<!--
Define your models in the list with mapping tag, LitePal will
create tables for each mapping class. The supported fields
defined in models will be mapped into columns.
For example:
<list>
<mapping class="com.test.model.Reader" />
<mapping class="com.test.model.Magazine" />
</list>
-->
<list>
</list>
<!--
Define where the .db file should be. "internal" means the .db file
will be stored in the database folder of internal storage which no
one can access. "external" means the .db file will be stored in the
path to the directory on the primary external storage device where
the application can place persistent files it owns which everyone
can access. "internal" will act as default.
For example:
<storage value="external" />
-->
</litepal>
3、配置LitePalApplication
在 androidManifest.xml 文件中配置如下:
<manifest>
<application
android:name="org.litepal.LitePalApplication"
...
>
...
</application>
</manifest>
如果这一步出现 LitePalApplication 标红的情况,需要重新同步一下。
当然这一步也可以不配置,在onCreate() 方法中添加:
public class MyOwnApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
LitePal.initialize(this);
}
...
}
LitePal.initialize() 方法必须传入Context参数,而不能是activity 或者 service 实例。这样的话必须经常传入参数Context,十分繁琐。
以上是关于Android配置LitePal的主要内容,如果未能解决你的问题,请参考以下文章
Android:日常学习笔记(10)———使用LitePal操作数据库