欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2011.09.26——— android sample之Notepad(notepadprovider) androidsample 

程序员文章站 2022-07-15 14:43:34
...
2011.09.26——— android sample之Notepad(notepadprovider)

1、SQLiteQueryBuilder
SQLiteQueryBuilder 是一个构造SQL查询语句的辅助类。

首先,
qb.setTables(DIARY_TABLE_NAME)


然后
qb.setProjectionMap(Map)
这个事设置表明的映射
例如
Map.put(Notes._ID, Notes._ID);
Map.put(Notes.Name, Notes.Title + " as " +Notes.ID);

接着
qb.appendWhere();
选着性的 主要用于查询单个记录
qb.appendWhere(Notes._ID + "=" + uri.getPathSegments().get(1));

最后
SQLiteDatabase db = mOpenHelper.getReadableDatabase()//得到一个可读的SQLiteDatabase 实例。
Cursor c = qb.query(db, projection, selection, selectionArgs, null,null, orderBy)

这个查询类似于一个标准的SQL查询,但是这个查询是SQLiteQueryBuilder 来发起的,而不是SQLiteDatabase 直接发起的,所以在参数方面略有不同。这个函数为 query(SQLiteDatabase db, String[] projectionIn, String selection, String[] selectionArgs, String groupBy, String having, String sortOrder, String limit)下边将各个参数介绍一下。

第一个参数为要查询的数据库实例。

第二个参数是一个字符串数组,里边的每一项代表了需要返回的列名。

第三个参数相当于SQL语句中的where部分。

第四个参数是一个字符串数组,里边的每一项依次替代在第三个参数中出现的问号(?)。

第五个参数相当于SQL语句当中的groupby部分。

第六个参数相当于SQL语句当中的having部分。

第七个参数描述是怎么进行排序。

第八个参数相当于SQL当中的limit部分,控制返回的数据的个数。

2、cursor.setNotificationUri
参考:http://blog.chinaunix.net/space.php?uid=20665441&do=blog&id=1742298

setNotificationUri通常用在ContentProvider.query(),c.setNotificationUri(getContext().getContentResolver(), uri); 是用在cursor被建立后,如果內容被改变时,cursor会自已知道已经不是最新状态而自行requery.

源码里面也是建一个SelfContentObserver,调用registerContentObserver()方法来实现的

3、getType
参考:http://hi.baidu.com/ljlkings/blog/item/b5e752c9410103ee53664f4a.html

返回目前操作的数据的MIME类型

也就是说你想通过mime类型类调用组件的时候 就需要实现getType()
<intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.intent.action.EDIT" />
                <action android:name="android.intent.action.PICK" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
 </intent-filter>


@Override
    public String getType(Uri uri) {
        switch (sUriMatcher.match(uri)) {
        case NOTES:
        case LIVE_FOLDER_NOTES:
            return Notes.CONTENT_TYPE;

        case NOTE_ID:
            return Notes.CONTENT_ITEM_TYPE;

        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
    }


一般来说:

如果操作的数据属于集合类型,那么MIME类型字符串应该以vnd.android.cursor.dir/开头,
例如:要得到所有person记录的Uri为content://cn.itcast.provider.personprovider/person,
那么返回的MIME类型字符串应该为:“vnd.android.cursor.dir/person”。
如果要操作的数据属于非集合类型数据,那么MIME类型字符串应该以vnd.android.cursor.item/开头,
例如:得到id为10的person记录,Uri为content://cn.itcast.provider.personprovider/person/10,
那么返回的MIME类型字符串应该为:“vnd.android.cursor.item/person”。

相关标签: android sample