马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 alltolove 于 2017-10-21 08:58 编辑
修改build.gradle文件下dependencies,添加compile 'com.android.support:appcompat-v7:26.+'
然后同步一下代码。
然后在main文件夹下新建assets文件夹,在assets文件夹下新建litepal.xml文件<?xml version="1.0" encoding="utf-8" ?>
<litepal>
<dbname value="BookStore">
</dbname>
<version value="1">
</version>
<list>
</list>
</litepal>
打开AndroidManifest.xml在application标签属性里添加android:name="org.litepal.LitePalApplication"
修改activity_main.xml文件<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/create_database"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="create database"/>
<Button
android:id="@+id/add_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add data"/>
<Button
android:id="@+id/update_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Update data"/>
<Button
android:id="@+id/delete_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete data"/>
<Button
android:id="@+id/query_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Query data"/>
</LinearLayout>
在mainactivity.java文件旁新建Book.java文件package com.example.xinwei.mylitepal;
/**
* Created by xinwei on 2017/10/21.
*/
public class Book {
private int id;
private String anthor;
private double price;
private int pages;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAnthor() {
return anthor;
}
public void setAnthor(String anthor) {
this.anthor = anthor;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
然后接着修改litepal.xml文件代码,在list标签下添加一行<mapping class="com.example.xinwei.mylitepal.Book"></mapping>
这里面的class属性要用你自己的包名,也就是Book类所在的位置。
然后修改mainactivity.java代码package com.example.xinwei.mylitepal;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import org.litepal.tablemanager.Connector;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button createDatabase = (Button)findViewById(R.id.create_database);
createDatabase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Connector.getDatabase();
}
});
}
}
这样数据库就创建好了,然后我们给数据库更新下数据,在Book类里添加private String press;
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
在mainactivity.java旁边新建个Category.javapackage com.example.xinwei.mylitepal;
/**
* Created by xinwei on 2017/10/21.
*/
public class Category {
private int id;
private String categoryName;
private int categoryCode;
public void setId(int id) {
this.id = id;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public void setCategoryCode(int categoryCode) {
this.categoryCode = categoryCode;
}
}
修改litepal.xml文件,在list标签下添加<mapping class="com.example.xinwei.mylitepal.Category"></mapping>
|