`
- 浏览:
12937266 次
- 性别:
- 来自:
大连
-
android.widget.CursorAdapter
它首先实现了两个接口<wbr style="line-height:25px"><span style="color:#993300; line-height:25px">Filterable</span><wbr style="line-height:25px">,CursorFilter.<wbr style="line-height:25px"><span style="color:#993300; line-height:25px">CursorFilterClient</span><wbr style="line-height:25px">。<br style="line-height:25px">
其中Filterable接口定义了getFilter()这个接口。<br style="line-height:25px">
CursorFilterClient定义<span style="line-height:25px"><wbr style="line-height:25px">如下接口</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">CharSequenceconvertToString(Cursorcursor);<br style="line-height:25px">
CursorrunQueryOnBackgroundThread(CharSequenceconstraint);<br style="line-height:25px">
CursorgetCursor();<br style="line-height:25px">
voidchangeCursor(Cursorcursor);</span><br style="line-height:25px">
注意:CursorFilter.CursorFilterClient是<wbr style="line-height:25px">非public的<wbr style="line-height:25px">,所以只有文档中看不到它,在源码中才能看到它。<br style="line-height:25px">
CursorAdapter.java文件如下<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">packageandroid.widget;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">-----------------------------------省略---------------------------------</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicabstractclass</span><span style="color:#ff00ff; line-height:25px">CursorAdapter</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">extends</span><span style="color:#3366ff; line-height:25px">BaseAdapter</span><span style="color:#993300; line-height:25px">implements</span><span style="color:#3366ff; line-height:25px">Filterable,</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">CursorFilter.CursorFilterClient{</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**<br style="line-height:25px">
*Thisfieldshouldbemadeprivate,soitishiddenfromtheSDK.<br style="line-height:25px">
*{@hide}<br style="line-height:25px">
*/</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">protectedbooleanmDataValid;</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**<br style="line-height:25px">
*Thisfieldshouldbemadeprivate,soitishiddenfromtheSDK.<br style="line-height:25px">
*{@hide}<br style="line-height:25px">
*/</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">protectedbooleanmAutoRequery;</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**<br style="line-height:25px">
*Thisfieldshouldbemadeprivate,soitishiddenfromtheSDK.<br style="line-height:25px">
*{@hide}<br style="line-height:25px">
*/</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">protectedCursormCursor;</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">-----------------------------------省略---------------------------------</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">protectedCursorFiltermCursorFilter;</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**<br style="line-height:25px">
*Thisfieldshouldbemadeprivate,soitishiddenfromtheSDK.<br style="line-height:25px">
*{@hide}<br style="line-height:25px">
*/</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">protectedFilterQueryProvidermFilterQueryProvider;</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">-----------------------------------省略---------------------------------</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicCursorgetCursor(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnmCursor;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">-----------------------------------省略---------------------------------</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidchangeCursor(Cursorcursor){</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">-----------------------------------省略---------------------------------</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">-----------------------------------省略---------------------------------</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicCharSequenceconvertToString(Cursorcursor){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returncursor==null?"":cursor.toString();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#333333; line-height:25px">-----------------------------------省略---------------------------------</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">CursorrunQueryOnBackgroundThread(CharSequenceconstraint){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(mFilterQueryProvider!=null){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">mFilterQueryProvider.runQuery(constraint);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">mCursor;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">FiltergetFilter(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(mCursorFilter==null){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">mCursorFilter=newCursorFilter(this);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnmCursorFilter;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">-----------------------------------省略---------------------------------</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicFilterQueryProvidergetFilterQueryProvider(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnmFilterQueryProvider;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">-----------------------------------省略---------------------------------</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidsetFilterQueryProvider(FilterQueryProviderfilterQueryProvider){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">mFilterQueryProvider=filterQueryProvider;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#333333; line-height:25px">-----------------------------------省略---------------------------------</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px">
CursorFilter文件如下:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">packageandroid.widget;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.database.Cursor;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#ff00ff; line-height:25px">CursorFilter</span><span style="color:#993300; line-height:25px">extends</span><span style="color:#3366ff; line-height:25px">Filter{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">CursorFilterClientmClient;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">interface</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">CursorFilterClient</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">CharSequenceconvertToString(Cursorcursor);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">CursorrunQueryOnBackgroundThread(CharSequenceconstraint);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">CursorgetCursor();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">voidchangeCursor(Cursorcursor);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">CursorFilter(CursorFilterClientclient){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">mClient=client;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicCharSequenceconvertResultToString(ObjectresultValue){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnmClient.convertToString((Cursor)resultValue);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">protectedFilterResultsperformFiltering(CharSequenceconstraint){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Cursorcursor=mClient.runQueryOnBackgroundThread(constraint);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">FilterResultsresults=newFilterResults();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(cursor!=null){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">results.count=cursor.getCount();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">results.values=cursor;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}else{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">results.count=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">results.values=null;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnresults;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">protectedvoidpublishResults(CharSequenceconstraint,FilterResultsresults){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">CursoroldCursor=mClient.getCursor();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(results.values!=null&&results.values!=oldCursor){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">mClient.changeCursor((Cursor)results.values);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px">
关于Filter的更多内容参见《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201010293507688/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">关键字过滤器Filter</a></strong>》。<br style="line-height:25px"><wbr style="line-height:25px">如何使用Filte<wbr style="line-height:25px">r呢?<br style="line-height:25px">
调用CursorAdapter的getFilter方法得到一个Filter,调用它的filter(CharSequenceconstraint)方法。其中constraint就是关键字。<br style="line-height:25px">
注意1:如果不给CursorAdapter设置FilterQueryProvider(通过setFilterQueryProvider方法),<br style="line-height:25px">
那么就需要<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">重载</span><wbr style="line-height:25px"><span style="color:#0000ff; line-height:25px">Cursor</span><span style="color:#993300; line-height:25px"><wbr style="line-height:25px">runQueryOnBackgroundThread</wbr></span><wbr style="line-height:25px"><span style="color:#0000ff; line-height:25px">(CharSequenceconstraint)</span>来实现对数据库的过滤查询。<br style="line-height:25px">
当然FilterQueryProvider其实就是在它唯一的方法<span style="color:#0000ff; line-height:25px">publicabstractCursor</span><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">runQuery</span><wbr style="line-height:25px"><span style="color:#0000ff; line-height:25px">(CharSequenceconstraint)</span>里面实现对数据库的过滤查询。<br style="line-height:25px">
参考源代码可以知道如果提供了FilterQueryProvider,那么CursorAdapter就是在runQueryOnBackgroundThread方法中直接调用FilterQueryProvider的<wbr style="line-height:25px"><span style="color:#ff6600; line-height:25px">runQuery</span><wbr style="line-height:25px">方法来进行数据库的过滤查询</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
安卓开发-继承CursorAdapter实现自己的功能。这里就是往sqlite数据库插入数据,然后提取出数据并展示到listview中。这里仅仅是为了学习的目的。其实这里完全可以使用SimpleCursorAdapter来做。
a simple demo about the use of CursorAdapter on Android
CursorAdapter和ListView结合使用,相关博文:http://www.cnblogs.com/tianzhijiexian/p/4019645.html
android demo,listview,cursorAdapter的使用,
CursorAdapter的用法 简单的CursorAdapter的用法 简单入门
androidx-cursoradapter-1.0.0.aar
使用CursorAdapter写了一个小的应用
VFP 通过CursorAdapter 连接access的各种方式
我从网络下载的有关 CursorAdapter(CA)相关资料,其中有: 介绍CursorAdapter类 CursorAdapter 起步 国外FoxPro MVP的CursorAdapter示例 VFP9对CursorAdapter的改进 vfp9CA的新方法 CursorAdapter类的制作和应用1-2...
NULL 博文链接:https://zymic.iteye.com/blog/743538
到处可以看到VFP专家Doug Hennig的大作--CursorAdapter起步,但很难看到其中说到的附件(SFDataClasses.vcx),经常看到有人要这个类,但找不到。这个是我花了些精力和时间弄到的,有需要的Foxer,可以看看。
NULL 博文链接:https://zhuqiang5138.iteye.com/blog/1356403
内容概要: ...能学到什么:ContentProvider、ContentResolver、CursorAdapter、Cursor、后台服务、前台服务、绑定服务、自定义广播、播放进度条、OnClickListener监听器 适用人群:Android开发初学者
CursorAdapter使用1
CursorAdapter的demo,作用是查询出数据库中到联系人名字(注意没有联系人就没有数据)
支持RecyclerView的CursorAdapter,包装了RecyclerView.Adapter的notifyDataSetChanged()相关系列方法。当数据发生改变的时候,只需要调用swapCursorItemChanged()等方法就行了。
可以使用 SQLite 数据库进行存储,并通过 Content Provider 和 CursorAdapter 等技术实现数据的查询和展示。 实现功能模块:包括用户登录注册、商品发布、商品搜索、购买下单、交易评价等功能模块。在实现这些功能...
该项目演示了如何将 ListViewAnimations 与 CursorAdapter 一起使用。 如果使用CursorAdapter启用拖放功能,则需要对adatper进行一些更改。 基本上,拖放涉及对列表的中间修改,在用户完成拖放操作之前将这些更改...