`
kanwoerzi
  • 浏览: 1646187 次
文章分类
社区版块
存档分类
最新评论

android中的Cursor

 
阅读更多
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 viarequery(),deactivate(), orclose().
其对应的取消监听接口如下:
<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 viaregisterContentObserver(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 viaregisterContentObserver(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进行管理的机制:
publicvoidstartManagingCursor(Cursorc)
Since:APILevel1
ThismethodallowstheactivitytotakecareofmanagingthegivenCursor'slifecycle
foryoubasedontheactivity'slifecycle.Thatis,whentheactivityisstopped
itwillautomaticallycalldeactivate()onthegivenCursor,
andwhenitislaterrestarteditwillcallrequery()foryou.
Whentheactivityisdestroyed,allmanagedCursorswillbeclosedautomatically
.
参数
cTheCursortobemanaged.
publicfinalCursormanagedQuery(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的用法

    Android cursor的详细说明,对于新手来说比较有用

    android 使用 Cursor 访问数据库 生成 ListView 并获取选中项的值

    android 使用 Cursor 访问数据库 生成 ListView 并获取选中项的值

    Android开发笔记之:深入理解Cursor相关的性能问题

    本篇文章是对Android中Cursor相关的性能问题进行了详细的分析介绍,需要的朋友参考下

    Android检测Cursor泄漏的原理以及使用方法

    本文介绍如何在 Android 检测 Cursor 泄漏的原理以及使用方法,还指出几种常见的出错示例,同时该方法同样适合于其他需要检测资源泄露的情况,感兴趣的朋友可以了解下

    android-mouse-cursor:在 Android 中使用 AccessibilityService 模拟鼠标(无需 root)

    在 Android Studio 中打开项目并在您的设备上运行。 前往“设置”&gt;“辅助功能”并打开“鼠标光标”。 将mouse_udp_client.py的 IP 地址编辑为您的 Android 设备的 IP 地址 运行mouse_udp_client.py并使用屏幕上的...

    Cursor的用法.docx

    本文简述了android的cursor的用法,通过本文你可以熟悉的掌握cursor的用法,通过实际的例程讲解

    android中跨进程通讯的4种方式

    由于android系统中应用程序之间不能共享内存。因此,在不同应用程序之间交互数据(跨进程通讯)就稍微麻烦一些。在android SDK中提供了4种用于跨进程通讯的方式。这4种方式正好对应于android系统中4种应用程序组件:...

    android在异步任务中关闭Cursor的代码方法

    android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现

    SQLLite学习运用

    sqlite数据库进阶 .doc查询语句返回的结果都是Cursor,但对于Cursor数据类型,各大数据库的使用方法...Cursor是一个泛型数据集,尽管android中Cursor数据的读取方式比较灵活,不过一般还是需要遵循以下一个必要的步骤:

    Android Menu和ListView 整合设计 源码

    import android.database.Cursor; import android.database.sqlite.SQLiteCursor; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android....

    Android App调试内存泄露之Cursor.zip

    Android App调试内存泄露之Cursor.zip

    Android学习资料

    包括:android gravity 和 android layout_Gravity,Bundle,ContentResolver,cursor,Fragments ,Fragments,inflater ,Fragment,数据查询query函数参数解析, 【Cursor】 类的介绍,android 获得通话记录,Handler ,...

    Android 实现读写U盘文件

    Android 基于libaums实现读写U盘文件,相关博客:https://blog.csdn.net/a512337862/article/details/80505128

    深入理解Android:卷2,中文完整扫描版

    《深入理解Android:卷2》不仅继承了第1本书的优点并改正了其在细微处存在的一些不足,而且还在写作的总体思想上进行了创新,更强调从系统设计者的角度去分析Android系统中各个模块内部的实现原理和工作机制。...

    Android掌上点餐系统

    import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DB_Table_Number extends SQLiteOpenHelper { private ...

    Android入门到精通源代码.

    2.4.2 安装和配置Eclipse中Android插件 2.5 使用Eclipse开发Android应用程序 2.5.1 使用Eclipse创建Android项目 2.5.2 Eclipse中Android项目架构 2.5.3 Eclipse中Android项目的调试和运行 第3章 Android中的Activity...

    Android高级编程--源代码

    在每章的讲解中,它会让你通过一系列示例项目逐步掌握Android中的各种新功能和技术,助你取得最圆满的学习效果。本书所介绍的各个应用实例简明扼要且极具实用价值,它们覆盖了Android 1.0的所有基本功能和高级功能...

Global site tag (gtag.js) - Google Analytics