前几天写了一个Android记事本小程序,现在记录一下。
项目源码地址在文章最后。
考虑到是记事本小程序,记录的内容只有文字,而且内容不会太长,所以选择使用SQLite数据库,数据存放在用户的手机上。
牵涉到数据库,那自然是一个实体。先设计实体数据表:DBHelper.java
package com.ikok.notepad.DBUtil;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Anonymous on 2016/3/24.
*/
public class DBHelper extends SQLiteOpenHelper {
/**
* 创建笔记表
*/
private static final String CREATE_NOTE = "create table Note(" +
"id integer primary key autoincrement," +
"content text," +
"time text)";
private Context mContext;
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_NOTE);
// Toast.makeText(mContext,"Created",Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
创建完数据表后,自然需要操作数据库,CRUD数据,我把所有跟数据库有关的操作封装在一起: NoteDB.java
package com.ikok.notepad.DBUtil;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.ikok.notepad.Entity.Note;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Anonymous on 2016/3/24.
*/
public class NoteDB {
public static final String DB_NAME = "notepad";
public static final int VERSION = 1;
private static NoteDB mNoteDB;
private SQLiteDatabase db;
public NoteDB(Context context) {
DBHelper dbHelper = new DBHelper(context,DB_NAME,null,VERSION);
db = dbHelper.getWritableDatabase();
}
/**
* 获取 NoteDB 的实例
* @param context
* @return
*/
public synchronized static NoteDB getInstance(Context context){
if (mNoteDB == null){
mNoteDB = new NoteDB(context);
}
return mNoteDB;
}
public void saveNote(Note note){
if (note != null) {
ContentValues values = new ContentValues();
values.put("content", note.getContent());
values.put("time", note.getTime());
db.insert("Note", null, values);
}
}
public List<Note> loadNotes(){
List<Note> noteList = new ArrayList<Note>();
/**
* 先按时间降序排列,再按id降序排列
*/
Cursor cursor = db.query("Note",null,null,null,null,null,"time desc,id desc");
if (cursor.moveToNext()){
do {
Note note = new Note();
note.setId(cursor.getInt(cursor.getColumnIndex("id")));
note.setContent(cursor.getString(cursor.getColumnIndex("content")));
note.setTime(cursor.getString(cursor.getColumnIndex("time")));
noteList.add(note);
} while (cursor.moveToNext());
}
return noteList;
}
public Note loadById(int id){
Note note = null;
Cursor cursor = db.query("Note",null,"id = " + id,null,null,null,null);
if (cursor.moveToNext()){
note = new Note();
note.setContent(cursor.getString(cursor.getColumnIndex("content")));
note.setTime(cursor.getString(cursor.getColumnIndex("time")));
}
return note;
}
public void deleteById(Integer id){
db.delete("Note","id = " + id,null);
}
public void deleteAllNote(){
db.delete("Note", null, null);
}
public void updateById(String noteTime, String noteContent, int noteId){
ContentValues values = new ContentValues();
values.put("content",noteContent);
values.put("time",noteTime);
db.update("Note",values,"id = " + noteId,null);
}
}
package com.ikok.notepad.Entity;
import java.io.Serializable;
/**
* Created by Anonymous on 2016/3/24.
*/
public class Note implements Serializable {
private int id;
private String content;
private String time;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
接下来进行App主页的设计: main_activity.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:background="@drawable/repeat_bg"
android:orientation="vertical">
<TextView
android:id="@+id/app_title"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="16sp"
android:gravity="center"
android:text="@string/app_title"
android:textColor="#333"
/>
<ListView
android:id="@+id/listview"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ListView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
>
<ImageButton
android:id="@+id/about_btn"
android:src="@drawable/about_me"
android:layout_alignParentLeft="true"
android:paddingLeft="20dp"
android:background="#00ffffff"
android:scaleType="center"
android:layout_marginTop="4dp"
android:layout_width="52dp"
android:layout_he