当前位置:   article > 正文

简单的记事本(SQLite+自定义adapter)_c#sqlite实现记事本功能源代码

c#sqlite实现记事本功能源代码

初学者学习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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

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);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/472235
推荐阅读
相关标签
  

闽ICP备14008679号