`
- 浏览:
1643351 次
-
CursorAdapter<wbr style="line-height:25px">继承于BaseAdapter,它是个虚类,它为cursor和ListView提供了连接的桥梁。<br style="line-height:25px">
publicabstractclass<br style="line-height:25px">
CursorAdapter<br style="line-height:25px">
extendsBaseAdapter<br style="line-height:25px">
直接子类只有ResourceCursorAdapter<br style="line-height:25px">
ClassOverview<br style="line-height:25px"><span style="color:#003366; line-height:25px">AdapterthatexposesdatafromaCursortoaListViewwidget.<br style="line-height:25px">
TheCursormustincludeacolumnnamed"_id"orthisclasswillnotwork.</span><br style="line-height:25px">
注意cursor的必须要有个命名为"<span style="color:#993300; line-height:25px">_id</span>"的列。比如Contacts._ID就为"<span style="color:#993300; line-height:25px">_id</span>"<br style="line-height:25px"><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:#993300; line-height:25px">abstractView</span><span style="color:#ff6600; line-height:25px">newView</span>(Contextcontext,Cursorcursor,ViewGroupparent)<br style="line-height:25px">
Makesanewviewtoholdthedatapointedtobycursor.<br style="line-height:25px"><span style="color:#993300; line-height:25px">abstractvoid</span><span style="color:#ff6600; line-height:25px">bindView</span>(Viewview,Contextcontext,Cursorcursor)<br style="line-height:25px">
Bindanexistingviewtothedatapointedtobycursor<br style="line-height:25px"><span style="line-height:25px">注意</span>:<br style="line-height:25px">
newView该函数第一次回调用后,如果数据增加后也会再调用,但是重绘是不会调用的。<br style="line-height:25px">
数据增加后,回调用该函数来生成与新增数据相对应的view。<br style="line-height:25px">
bindView函数第一次回调用后,如果数据更新也会再调用,但重绘会再次调用的。<br style="line-height:25px">
【总的来说应该是在调用bindView如果发现view为空会先调用newView来生成view】<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.List;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.app.Activity;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.app.ListActivity;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.os.Bundle;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.os.Handler;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.content.Context;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.content.ContentValues;</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:#3366ff; line-height:25px">importandroid.view.LayoutInflater;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.view.View;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.widget.ListView;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.view.ViewGroup;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.widget.ArrayAdapter;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.widget.CursorAdapter;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.widget.TextView;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.provider.ContactsContract.Contacts;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.provider.ContactsContract.RawContacts;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.view.View.OnClickListener;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.widget.Button;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">HelloCursor</span><span style="color:#3366ff; line-height:25px">extendsListActivity{<br style="line-height:25px">
privatestaticString[]PROJECTION=newString[]{Contacts._ID,<br style="line-height:25px">
Contacts.DISPLAY_NAME};<br style="line-height:25px"><br style="line-height:25px"></span><span style="color:#808080; line-height:25px">/**Calledwhentheactivityisfirstcreated.*/<br style="line-height:25px">
@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onCreate</span><span style="color:#3366ff; line-height:25px">(BundlesavedInstanceState){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onCreate(savedInstanceState);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">setContentView(R.layout.main);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Cursorc=getContentResolver().query(Contacts.CONTENT_URI,PROJECTION,</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">null,null,Contacts.DISPLAY_NAME+"COLLATENOCASE");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">startManagingCursor(c);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">MyCursorAdapteradapter=newMyCursorAdapter(this,R.layout.list_row,</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">c);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this.setListAdapter(adapter);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Buttonbutton=(Button)findViewById(R.id.Button01);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">OnClickListenerlistener=newOnClickListener(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">@Override</span><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidonClick(Viewv){</span><span style="color:#3366ff; line-height:25px"><br style="line-height:25px">
doAction();</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">button.setOnClickListener(listener);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">mHandler=newHandler();</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"><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateString[]mStrings={"hubin","hudashi","robin"};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">intcnt=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateHandlermHandler;</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">class</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">AddContactThread</span><span style="color:#3366ff; line-height:25px">implementsRunnable{<br style="line-height:25px">
publicvoidrun(){<br style="line-height:25px">
intnStringLength=mStrings.length;<br style="line-height:25px">
intrandomNumber=0;<br style="line-height:25px">
ContentValuesnewValues=newContentValues();<br style="line-height:25px">
StringtempString=null;<br style="line-height:25px">
randomNumber=(int)(Math.random()%10);<br style="line-height:25px">
for(inti=0;i<nStringLength;i++){<br style="line-height:25px">
tempString=mStrings<wbr style="line-height:25px">+cnt+randomNumber;<br style="line-height:25px">
newValues.put(Contacts.DISPLAY_NAME,tempString);<br style="line-height:25px">
getContentResolver().insert(RawContacts.CONTENT_URI,newValues);<br style="line-height:25px">
newValues.clear();<br style="line-height:25px"><br style="line-height:25px">
}<br style="line-height:25px">
cnt++;<br style="line-height:25px">
}<br style="line-height:25px">
}<br style="line-height:25px">
AddContactThreadaddContact=newAddContactThread();<br style="line-height:25px">
void</wbr></span><span style="color:#ff6600; line-height:25px">doAction</span><span style="color:#3366ff; line-height:25px">()<br style="line-height:25px">
{<br style="line-height:25px">
mHandler.post(addContact);<br style="line-height:25px">
}<br style="line-height:25px">
}<br style="line-height:25px">
class</span><span style="color:#993300; line-height:25px">MyCursorAdapter</span><span style="color:#3366ff; line-height:25px">extendsCursorAdapter{<br style="line-height:25px">
Contextcontext=null;<br style="line-height:25px">
intviewResId;<br style="line-height:25px">
publicMyCursorAdapter(Contextcontext,intresource,Cursorcursor){<br style="line-height:25px">
super(context,cursor);<br style="line-height:25px">
viewResId=resource;<br style="line-height:25px">
}<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicView</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">newView</span><span style="color:#3366ff; line-height:25px">(Contextcontext,Cursorcursor,ViewGroupparent){</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">TextViewview=null;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">LayoutInflatervi=null;</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">vi=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);<br style="line-height:25px">
view=(TextView)vi.inflate(viewResId,parent,false);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">//v=(TextView)vi.inflate(textViewResourceId,null);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i("hubin","newView"+view);</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">view;</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:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">bindView</span><span style="color:#3366ff; line-height:25px">(Viewview,Contextcontext,Cursorcursor){<br style="line-height:25px">
Log.i("hubin","bind"+view);<br style="line-height:25px">
TextViewnameView=(TextView)view;<br style="line-height:25px">
//Setthename<br style="line-height:25px">
nameView.setText(cursor<br style="line-height:25px">
.getString(cursor.getColumnIndex("DISPLAY_NAME")));<br style="line-height:25px">
}<br style="line-height:25px">
}</span><br style="line-height:25px"><span style="line-height:25px">附1:</span>关于newView和bindView一测试结果<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">newViewandroid.widget.TextView@43b98ea0<br style="line-height:25px">
bindandroid.widget.TextView@43b98ea0<br style="line-height:25px">
newViewandroid.widget.TextView@43b99948<br style="line-height:25px">
bindandroid.widget.TextView@43b99948<br style="line-height:25px">
newViewandroid.widget.TextView@43b9a3f0<br style="line-height:25px">
bindandroid.widget.TextView@43b9a3f0<br style="line-height:25px">
add<br style="line-height:25px">
bindandroid.widget.TextView@43b9a3f0<br style="line-height:25px">
bindandroid.widget.TextView@43b99948<br style="line-height:25px">
bindandroid.widget.TextView@43b98ea0<br style="line-height:25px">
newViewandroid.widget.TextView@43b9c5b0<br style="line-height:25px">
bindandroid.widget.TextView@43b9c5b0<br style="line-height:25px">
newViewandroid.widget.TextView@43b9d058<br style="line-height:25px">
bindandroid.widget.TextView@43b9d058<br style="line-height:25px">
newViewandroid.widget.TextView@43b9db00<br style="line-height:25px">
bindandroid.widget.TextView@43b9db00</span></wbr></wbr>
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
CursorAdapter和ListView结合使用,相关博文:http://www.cnblogs.com/tianzhijiexian/p/4019645.html
android demo,listview,cursorAdapter的使用,
androidx-cursoradapter-1.0.0.aar
内容概要: ...能学到什么:ContentProvider、ContentResolver、CursorAdapter、Cursor、后台服务、前台服务、绑定服务、自定义广播、播放进度条、OnClickListener监听器 适用人群:Android开发初学者
使用CursorAdapter写了一个小的应用
我从网络下载的有关 CursorAdapter(CA)相关资料,其中有: 介绍CursorAdapter类 CursorAdapter 起步 国外FoxPro MVP的CursorAdapter示例 VFP9对CursorAdapter的改进 vfp9CA的新方法 CursorAdapter类的制作和应用1-2...
安卓开发-继承CursorAdapter实现自己的功能。这里就是往sqlite数据库插入数据,然后提取出数据并展示到listview中。这里仅仅是为了学习的目的。其实这里完全可以使用SimpleCursorAdapter来做。
到处可以看到VFP专家Doug Hennig的大作--CursorAdapter起步,但很难看到其中说到的附件(SFDataClasses.vcx),经常看到有人要这个类,但找不到。这个是我花了些精力和时间弄到的,有需要的Foxer,可以看看。
CursorAdapter的用法 简单的CursorAdapter的用法 简单入门
VFP 通过CursorAdapter 连接access的各种方式
NULL 博文链接:https://zymic.iteye.com/blog/743538
NULL 博文链接:https://zhuqiang5138.iteye.com/blog/1356403
CursorAdapter使用1
CursorAdapter的demo,作用是查询出数据库中到联系人名字(注意没有联系人就没有数据)
支持RecyclerView的CursorAdapter,包装了RecyclerView.Adapter的notifyDataSetChanged()相关系列方法。当数据发生改变的时候,只需要调用swapCursorItemChanged()等方法就行了。
该项目演示了如何将 ListViewAnimations 与 CursorAdapter 一起使用。 如果使用CursorAdapter启用拖放功能,则需要对adatper进行一些更改。 基本上,拖放涉及对列表的中间修改,在用户完成拖放操作之前将这些更改...
###使用了ContentProvider+LoaderManager+CursorAdapter,为ListView提供数据 ###为什么要这样做 传统的ListVIew更新数据操作麻烦,且在不同的线程中会产生问题 使用ContentProvider可以简化、结构化数据操作 通过...
android-simple-listview-app 简单的Android ListView用法(CursorAdapter)示例