noteslistactivity

This commit is contained in:
ziji Wu 2019-10-08 17:35:14 +08:00
parent 648f70b3b9
commit deb170941a
7 changed files with 464 additions and 398 deletions

File diff suppressed because it is too large Load Diff

View File

@ -35,7 +35,7 @@ import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.data.NotesDatabaseHelper.TABLE;
/**
*
*定义便签提供者类
*/
public class NotesProvider extends ContentProvider {
private static final UriMatcher mMatcher;
@ -81,18 +81,25 @@ public class NotesProvider extends ContentProvider {
+ " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER
+ " AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE;
//重写onCreate方法实例化类时调用
//获取便签数据库
@Override
public boolean onCreate() {
mHelper = NotesDatabaseHelper.getInstance(getContext());
return true;
}
//重写查询方法
//@ selection 查询条件之前的字符串常量
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
//游标置空
Cursor c = null;
//以可读方式打开数据库
SQLiteDatabase db = mHelper.getReadableDatabase();
String id = null;
//选择判断uri
switch (mMatcher.match(uri)) {
case URI_NOTE:
c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null,
@ -137,6 +144,7 @@ public class NotesProvider extends ContentProvider {
c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY,
new String[] { searchString });
} catch (IllegalStateException ex) {
//将错误信息写入日志便于查错
Log.e(TAG, "got exception: " + ex.toString());
}
break;
@ -149,8 +157,10 @@ public class NotesProvider extends ContentProvider {
return c;
}
//该方法实现了向特定uri地址插入内容
@Override
public Uri insert(Uri uri, ContentValues values) {
//以写入方式打开数据库
SQLiteDatabase db = mHelper.getWritableDatabase();
long dataId = 0, noteId = 0, insertedId = 0;
switch (mMatcher.match(uri)) {
@ -166,6 +176,7 @@ public class NotesProvider extends ContentProvider {
insertedId = dataId = db.insert(TABLE.DATA, null, values);
break;
default:
//uri无效抛出异常
throw new IllegalArgumentException("Unknown URI " + uri);
}
// Notify the note uri
@ -183,11 +194,13 @@ public class NotesProvider extends ContentProvider {
return ContentUris.withAppendedId(uri, insertedId);
}
//重写删除方法根据选择条件删除指定内容
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = 0;
String id = null;
SQLiteDatabase db = mHelper.getWritableDatabase();
//创建是否删除标识
boolean deleteData = false;
switch (mMatcher.match(uri)) {
case URI_NOTE:
@ -229,11 +242,14 @@ public class NotesProvider extends ContentProvider {
return count;
}
//重写更新方法更新数据库中的内容
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
int count = 0;
String id = null;
SQLiteDatabase db = mHelper.getWritableDatabase();
//创建是否更新标识
//false - 未更新
boolean updateData = false;
switch (mMatcher.match(uri)) {
case URI_NOTE:
@ -273,6 +289,7 @@ public class NotesProvider extends ContentProvider {
return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
}
//私有方法更新便签版本
private void increaseNoteVersion(long id, String selection, String[] selectionArgs) {
StringBuilder sql = new StringBuilder(120);
sql.append("UPDATE ");