android中的Cursor

本文介绍了 Android 中 Cursor 类提供的用于监听数据库变化的方法,包括 registerContentObserver 和 registerDataSetObserver 接口,以及如何取消监听和重新查询数据。同时,还讲解了 Activity 如何管理 Cursor 的生命周期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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还提供了重新查询的接口:
<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.
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进行管理。
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值