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

CursorAdapter

 
阅读更多
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&lt;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>
分享到:
评论
1 楼 chenqidou 2013-03-26  

相关推荐

Global site tag (gtag.js) - Google Analytics