目前初学android,也许很多人对于SQLite数据库的使用不是很清楚,主要对于查询这方面,直接先上图。
1.主界面,直接查询出所有数据
2,按类型查询
3,按作者查询
4,点击一首诗,出现对于的诗的完整信息
5,点击译文,注解都会再下面插入,再次点击撤销
可以上下拖动
下面我们就开始上代码和数据库建立
Activity:
MianActivity的activity_main布局,放入一个TabHost,TabHost分别跳转到3个Activity-全部(AllActiity),按类型(TypeActivity),按作者(PoetActivity)。
按类型(TypeActivity):两个类型分别跳转到-五言绝句(Type5Activity),七言绝句(Type7Activity)
按作者(PoetAtivity):加入一个自动文本AutoCompleteTextView进行搜索。
数据库:Poet(database)下有2个表:poet表( _id;poetname;poemname;poemtype;) content表(_id;poemname;poetname;poemcontent;poemyiwen;poemzhujie;)
实体类:
Poet.java:content.java
SQLiteOpenHelper:DBOpenHelper.java创建数据库
Dao.java:对表中数据进行操作的方法类
Poet.java:
package com.example.android_1800_sqlite1;
public class Poet {
int _id;
String poetname;
String poemname;
String poemtype;
public Poet() {
super();
// TODO Auto-generated constructor stub
}
public Poet(int _id, String poetname, String poemname, String poemtype) {
super();
this._id = _id;
this.poetname = poetname;
this.poemname = poemname;
this.poemtype = poemtype;
}
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String getPoetname() {
return poetname;
}
public void setPoetname(String poetname) {
this.poetname = poetname;
}
public String getPoemname() {
return poemname;
}
public void setPoemname(String poemname) {
this.poemname = poemname;
}
public String getPoemtype() {
return poemtype;
}
public void setPoemtype(String type) {
this.poemtype = type;
}
@Override
public String toString() {
return "Poet [_id=" + _id + ", poetname=" + poetname + ", poemname="
+ poemname + ", type=" + poemtype + "]";
}
}
content.java
package com.example.android_1800_sqlite1;
import java.io.Serializable;
public class Content implements Serializable{
int _id;
String poemname;
String poetname;
String poemcontent;
String poemyiwen;
String poemzhujie;
public Content() {
super();
// TODO Auto-generated constructor stub
}
public Content(int _id, String poemname, String poetname,
String poemcontent, String poemyiwen, String poemzhujie) {
super();
this._id = _id;
this.poemname = poemname;
this.poetname = poetname;
this.poemcontent = poemcontent;
this.poemyiwen = poemyiwen;
this.poemzhujie = poemzhujie;
}
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String getPoemname() {
return poemname;
}
public void setPoemname(String poemname) {
this.poemname = poemname;
}
public String getPoetname() {
return poetname;
}
public void setPoetname(String poetname) {
this.poetname = poetname;
}
public String getPoemcontent() {
return poemcontent;
}
public void setPoemcontent(String poemcontent) {
this.poemcontent = poemcontent;
}
public String getPoemyiwen() {
return poemyiwen;
}
public void setPoemyiwen(String poemyiwen) {
this.poemyiwen = poemyiwen;
}
public String getPoemzhujie() {
return poemzhujie;
}
public void setPoemzhujie(String poemzhujie) {
this.poemzhujie = poemzhujie;
}
@Override
public String toString() {
return "Content [_id=" + _id + ", poemname=" + poemname + ", poetname="
+ poetname + ", poemcontent=" + poemcontent + ", poemyiwen="
+ poemyiwen + ", poemzhujie=" + poemzhujie + "]";
}
}
MainActivity.java:
package com.example.android_1800_sqlite1;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.LocalActivityManager;
import android.co