android.database.Cursor提供了两个接口来监听数据库的变化;
其对应的取消监听接口如下:
| <nobr style="line-height:21px">abstract void</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/database/Cursor.html#registerContentObserver(android.database.ContentObserver)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">registerContentObserver</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/database/ContentObserver.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ContentObserver</a>observer)</nobr>
Register an observer that is called when changes happen to the content backing this cursor.
|
| <nobr style="line-height:21px">abstract void</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/database/Cursor.html#registerDataSetObserver(android.database.DataSetObserver)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">registerDataSetObserver</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/database/DataSetObserver.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">DataSetObserver</a>observer)</nobr>
Register an observer that is called when changes happen to the contents of the this cursors data set, for example, when the data set is changed via
requery(),
deactivate(), or
close().
|
| <nobr style="line-height:21px">abstract void</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/database/Cursor.html#unregisterContentObserver(android.database.ContentObserver)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">unregisterContentObserver</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/database/ContentObserver.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ContentObserver</a>observer)</nobr>
Unregister an observer that has previously been registered with this cursor via
registerContentObserver(ContentObserver).
|
| <nobr style="line-height:21px">abstract void</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/database/Cursor.html#unregisterDataSetObserver(android.database.DataSetObserver)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">unregisterDataSetObserver</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/database/DataSetObserver.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">DataSetObserver</a>observer)</nobr>
Unregister an observer that has previously been registered with this cursor via
registerContentObserver(ContentObserver).
|
Cursor还提供了重新查询的接口:
Activity还提供了对Cursor进行管理的机制:
publicvoid startManagingCursor(Cursorc)
Since:APILevel1
ThismethodallowstheactivitytotakecareofmanagingthegivenCursor'slifecycle
foryoubasedontheactivity'slifecycle.Thatis,whentheactivityisstopped
itwillautomaticallycalldeactivate()onthegivenCursor,
andwhenitislaterrestarteditwillcallrequery()foryou.
Whentheactivityisdestroyed,allmanagedCursorswillbeclosedautomatically.
参数
cTheCursortobemanaged.
publicfinalCursor managedQuery(Uriuri,String[]projection,Stringselection,String[]selectionArgs,StringsortOrder)
Since:APILevel1
Wrapperaroundquery(android.net.Uri,String[],String,String[],String)thatgivestheresultingCursortocallstartManagingCursor(Cursor)sothattheactivitywillmanageitslifecycleforyou.
参数
uriTheURIofthecontentprovidertoquery.
projectionListofcolumnstoreturn.
selectionSQLWHEREclause.
selectionArgsTheargumentstoselection,ifany?sarepesent
sortOrderSQLORDERBYclause.
返回
*TheCursorthatwasreturnedbyquery().
注意:managedQuery就是查询了再调用了startManagingCursor,让Activity对Cursor进行管理。
| <nobr style="line-height:21px">abstract boolean</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/database/Cursor.html#requery()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">requery</a></span>()</nobr>
This method is deprecated. Don't use this. Just request a new cursor, so you can do this asynchronously and update your list view once the new cursor comes back.
|
publicvoid startManagingCursor(Cursorc)
Since:APILevel1
ThismethodallowstheactivitytotakecareofmanagingthegivenCursor'slifecycle
foryoubasedontheactivity'slifecycle.Thatis,whentheactivityisstopped
itwillautomaticallycalldeactivate()onthegivenCursor,
andwhenitislaterrestarteditwillcallrequery()foryou.
Whentheactivityisdestroyed,allmanagedCursorswillbeclosedautomatically.
参数
cTheCursortobemanaged.
publicfinalCursor managedQuery(Uriuri,String[]projection,Stringselection,String[]selectionArgs,StringsortOrder)
Since:APILevel1
Wrapperaroundquery(android.net.Uri,String[],String,String[],String)thatgivestheresultingCursortocallstartManagingCursor(Cursor)sothattheactivitywillmanageitslifecycleforyou.
参数
uriTheURIofthecontentprovidertoquery.
projectionListofcolumnstoreturn.
selectionSQLWHEREclause.
selectionArgsTheargumentstoselection,ifany?sarepesent
sortOrderSQLORDERBYclause.
返回
*TheCursorthatwasreturnedbyquery().
注意:managedQuery就是查询了再调用了startManagingCursor,让Activity对Cursor进行管理。
Cursor还提供一个对数据变化进行监听的接口
| <nobr style="line-height:21px">abstract void</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/database/Cursor.html#setNotificationUri(android.content.ContentResolver,%20android.net.Uri)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">setNotificationUri</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/content/ContentResolver.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ContentResolver</a>cr,<a rel="nofollow" href="http://developer.android.com/reference/android/net/Uri.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Uri</a>uri)</nobr>
Register to watch a content URI for changes.
|
关于
setNotificationUri()函数的更多内容请参考《
ContentProvider》
本文介绍了 Android 中 Cursor 类提供的用于监听数据库变化的方法,包括 registerContentObserver 和 registerDataSetObserver 接口,以及如何取消监听和重新查询数据。同时,还讲解了 Activity 如何管理 Cursor 的生命周期。
1419

被折叠的 条评论
为什么被折叠?



