1.配置LitePal
编辑app/build.gradle 文件,在dependencies闭包中添加如下内容:
compile 'org.litepal.android:core:1.6.1'
导包成功,我们将LitePal成功引入到当前项目中,接下来就是配置litepal.xml文件:
右击app/src/main 目录->New->Directory.创建一个assets目录,然后在assets目录下在创建一个litepal.xml文件,接着编辑litepal.xml文件中的内容:
<litepal>
<dbname value="demo" />
<version value="1" />
<list>
<mapping class="com.example.myapplication13.Song" />
<mapping class="com.example.myapplication13.Album"/>
list>
litepal>
其中, 意思是指定数据库名,标签用于版本号
配置LitePalApplication(修改AndroidManifest.xml中的代码):
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication13">
<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/AppTheme">
<actiavity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
actiavity>
application>
manifest>
新建两个类 album 、 Song 来继承DataSupport
在里面通过面对对象的思维来实现功能 :
package com.example.myapplication13;
import org.litepal.annotation.Column;
import org.litepal.crud.DataSupport;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
/**
* Created by lenovo on 2018/4/2.
*/
public class Album extends DataSupport {
@Column(unique = true, defaultValue = "unknown")
private String name;
private float price;
private byte[] cover;
private List songs = new ArrayList();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public byte[] getCover() {
return cover;
}
public void setCover(byte[] cover) {
this.cover = cover;
}
public List getSongs() {
return songs;
}
public void setSongs(List songs) {
this.songs = songs;
}
}
package com.example.myapplication13;
import org.litepal.annotation.Column;
import org.litepal.crud.DataSupport;
/**
* Created by lenovo on 2018/4/2.
*/
public class Song extends DataSupport {
@Column(nullable = false)
private String name;
private int duration;
@Column(ignore = true)
private String uselessField;
private Album album;
// generated getters and setters.
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public String getUselessField() {
return uselessField;
}
public void setUselessField(String uselessField) {
this.uselessField = uselessField;
}
public Album getAlbum() {
return album;
}
public void setAlbum(Album album) {
this.album = album;
}
}
在布局里 我设置了 增删改查 按钮 来展现
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.myapplication13.MainActivity">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="50dp" />
<Button
android:text="添加"
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="50dp" />
<Button
android:text="修改"
android:id="@+id/xiugai_btn"
android:layout_width="match_parent"
android:layout_height="50dp" />
<Button
android:text="删除"
android:id="@+id/shanchu_btn"
android:layout_width="match_parent"
android:layout_height="50dp" />
<Button
android:text="查询"
android:id="@+id/chanxun_btn"
android:layout_width="match_parent"
android:layout_height="50dp" />
LinearLayout>
在Maintivity里 我设置了点击事件:
package com.example.myapplication13;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.litepal.LitePal;
import org.litepal.crud.DataSupport;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private String TAG = "";
private EditText editText;
private Button button;
private Button button_xiugai;
private Button button_shanchu;
private Button button_chaxun;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// SQLiteDatabase db= LitePal.getDatabase();
binID();
}
private void binID() {
editText = findViewById(R.id.edit_text);
button = findViewById(R.id.button);
editText.setOnClickListener(this);
button.setOnClickListener(this);
button_xiugai = findViewById(R.id.xiugai_btn);
button_shanchu = findViewById(R.id.xiugai_btn);
button_xiugai.setOnClickListener(this);
button_shanchu.setOnClickListener(this);
button_chaxun = findViewById(R.id.chanxun_btn);
button_chaxun.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button://添加
String albumNmae = editText.getText().toString();
float price = 109.9f;
Album album = new Album();
album.setName(albumNmae);
album.setPrice(price);
album.save();
break;
case R.id.xiugai_btn://修改
Album album1 = DataSupport.find(Album.class, 1);
album1.setName("111111");
album1.save();
break;
case R.id.shanchu_btn://删除
int tow = DataSupport.delete(Album.class, 2);
int row = DataSupport.deleteAll(Album.class, "id>?", "3");
Toast.makeText(this, " " + tow, Toast.LENGTH_SHORT).show();
break;
case R.id.chanxun_btn://查询
// Album A = DataSupport.find(Album.class, 1);
List albumList = DataSupport.where("name like ?","a%").order("name").find(Album.class);
for (Album a :
albumList) {
Log.e(TAG, "onClick: " + a.getName());
}
break;
}
}}