简单的记事本(SQLite+自定义adapter)

初学者学习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
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值