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>
它首先实现了两个接口<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>