赞
踩
初学者学习android,做这个小demo时遇到过很多挫折。具体不说了。就简单的说一下这个demo的思路。
1、写一个实体类,里面定义这个demo需要的内容,然后写getXX()和setXX()。
2、首先新建一个数据库类继承SQLiteOpenHelper。在这个类里面进行创建数据库,数据库版本,数据表。
3、然后新建一个类用来对数据库进行增删查改操作。
4、写一个适配器,是view能在listview上展示的桥梁。
5、对各个页面写代码实现功能。
1、写一个实体类,里面定义这个demo需要的内容,然后写getXX()和setXX()
public class note implements Serializable {
private int id;
private String title;
private String time;
private String content;
//必须要有无参构造函数
public note(){
}
//有参构造函数
public note(int id,String title,String time,String content){
this.id=id;
this.title=title;
this.time=time;
this.content=content;
}
public String getTitle(){
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTime(){
return time;
}
public void setTime(String time){
this.time=time;
}
public String getContent(){
return content;
}
public void setContent(String content){
this.content=content;
}
public Integer getId(){
return id;
}
public void setId(int id){
this.id=id;
}
}

2、新建一个数据库类继承SQLiteOpenHelper。在这个类里面进行创建数据库,数据库版本,数据表。
public class MyNoteHelper extends SQLiteOpenHelper{
//数据库名称
private static final String DATABASE_NAME="noteDB.db";
//数据库版本
private static int DATABASE_VERSION=1;
//构造函数
public MyNoteHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
//创建表
public static final String CREATE_TABLE="CREATE TABLE IF NOT EXISTS MyNotes2("+"id integer primary key autoincrement,"+"title TEXT,"+"time TEXT ,"+"content TEXT)";
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
db.execSQL("ALTER TABLE MyNotes2 ADD COLUMN other String");
onCreate(db);
}
}

2、新建一个类用来对数据库进行增删查改操作。
public class NoteManager {
private MyNoteHelper helper;
private SQLiteDatabase db;
private Context context;
public NoteManager(Context context){
helper= new MyNoteHelper(context);
db=helper.getWritableDatabase();//打开一个读写的数据库
this.context=context;
}
//插入
public boolean insert(note note){
db.beginTransaction();//开始事务锁
try {
db.execSQL("INSERT INTO MyNotes2 VALUES(?,?,?,?)",new Object[]{note.getId(),note.getTitle(),note.getTime(),note.getContent()});
db.setTransactionSuccessful();//完成事务锁
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}finally {
db.endTransaction();//结束事务锁
}
}
//删除
public void delete(note note){
db.delete("MyNotes2", "id=?", new String[]{String.valueOf(note.getId())});
}
//查询
public List<note> query() {
List<note> notes = new ArrayList<>();
Cursor c = queryItem();
while (c.moveToNext()) {
note note = new note();
note.setId(c.getInt(c.getColumnIndex("id")));
note.setTitle(c.getString(c.getColumnIndex("title")));
note.setContent(c.getString(c.getColumnIndex("content")));
note.setTime(c.getString(c.getColumnIndex("time")));
notes.add(note);
}
c.close();
return notes;
}
//更新
public boolean update(note note){
db.beginTransaction();
try {
ContentValues values=new ContentValues();
values.put("title",note.getTitle());
values.put("content",note.getContent());
values.put("time", note.getTime());
db.update("MyNotes2", values, "id=?", new

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。