|
|
@ -35,6 +35,15 @@ import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
import net.micode.notes.data.NotesDatabaseHelper.TABLE;
|
|
|
|
import net.micode.notes.data.NotesDatabaseHelper.TABLE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 为存储和获取数据提供接口。可以在不同的应用程序之间共享数据
|
|
|
|
|
|
|
|
* ContentProvider提供的方法
|
|
|
|
|
|
|
|
* query:查询
|
|
|
|
|
|
|
|
* insert:插入
|
|
|
|
|
|
|
|
* update:更新
|
|
|
|
|
|
|
|
* delete:删除
|
|
|
|
|
|
|
|
* getType:得到数据类型
|
|
|
|
|
|
|
|
*/
|
|
|
|
public class NotesProvider extends ContentProvider {
|
|
|
|
public class NotesProvider extends ContentProvider {
|
|
|
|
private static final UriMatcher mMatcher;
|
|
|
|
private static final UriMatcher mMatcher;
|
|
|
|
|
|
|
|
|
|
|
@ -79,12 +88,19 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
+ " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER
|
|
|
|
+ " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER
|
|
|
|
+ " AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE;
|
|
|
|
+ " AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 只在onCreate()中初始化Context
|
|
|
|
|
|
|
|
* 对mHelper进行实例化
|
|
|
|
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public boolean onCreate() {
|
|
|
|
public boolean onCreate() {
|
|
|
|
mHelper = NotesDatabaseHelper.getInstance(getContext());
|
|
|
|
mHelper = NotesDatabaseHelper.getInstance(getContext());
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
*查询uri在数据库中对应的位置
|
|
|
|
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
|
|
|
|
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
|
|
|
|
String sortOrder) {
|
|
|
|
String sortOrder) {
|
|
|
@ -112,6 +128,9 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case URI_SEARCH:
|
|
|
|
case URI_SEARCH:
|
|
|
|
case URI_SEARCH_SUGGEST:
|
|
|
|
case URI_SEARCH_SUGGEST:
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 不合法的参数异常
|
|
|
|
|
|
|
|
*/
|
|
|
|
if (sortOrder != null || projection != null) {
|
|
|
|
if (sortOrder != null || projection != null) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"do not specify sortOrder, selection, selectionArgs, or projection" + "with this query");
|
|
|
|
"do not specify sortOrder, selection, selectionArgs, or projection" + "with this query");
|
|
|
@ -147,6 +166,9 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
return c;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 插入一个uri
|
|
|
|
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Uri insert(Uri uri, ContentValues values) {
|
|
|
|
public Uri insert(Uri uri, ContentValues values) {
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
@ -181,6 +203,9 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
return ContentUris.withAppendedId(uri, insertedId);
|
|
|
|
return ContentUris.withAppendedId(uri, insertedId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 删除一个uri
|
|
|
|
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
|
|
|
int count = 0;
|
|
|
|
int count = 0;
|
|
|
@ -227,6 +252,9 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
return count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 更新一个uri
|
|
|
|
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
|
|
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
|
|
|
int count = 0;
|
|
|
|
int count = 0;
|
|
|
@ -267,6 +295,9 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
return count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
*将字符串解析成规定格式
|
|
|
|
|
|
|
|
*/
|
|
|
|
private String parseSelection(String selection) {
|
|
|
|
private String parseSelection(String selection) {
|
|
|
|
return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
|
|
|
|
return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -292,7 +323,7 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sql.append(selectString);
|
|
|
|
sql.append(selectString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** execSQL()方法可以执行insert、delete、update和CREATE TABLE之类有更改行为的SQL语句 */
|
|
|
|
mHelper.getWritableDatabase().execSQL(sql.toString());
|
|
|
|
mHelper.getWritableDatabase().execSQL(sql.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|