publicabstractclass
ResourceCursorAdapter
extendsCursorAdapter
java.lang.Object
android.widget.BaseAdapter
android.widget.CursorAdapter
android.widget.ResourceCursorAdapter
ClassOverview
AneasyadapterthatcreatesviewsdefinedinanXMLfile.
YoucanspecifytheXMLfilethatdefinestheappearanceoftheviews.
<wbr style="line-height:25px">ResourceCursorAdapter</wbr> 是虚类,它继承于 CursorAdapte r,它实现了 CursorAdapter 的 newView 方法
用户使用
ResourceCursorAdapter
必须继续它并实现以下函数:
关于
ResourceCursorAdapter
的更多内容可以参考《CursorAdapter
》<wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:25px">示例1</span>:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.app.Activity;<br style="line-height:25px"> importandroid.app.ListActivity;<br style="line-height:25px"> importandroid.os.Bundle;<br style="line-height:25px"> importandroid.os.Handler;<br style="line-height:25px"> importandroid.content.Context;<br style="line-height:25px"> importandroid.content.ContentValues;<br style="line-height:25px"> importandroid.database.Cursor;<br style="line-height:25px"> importandroid.view.LayoutInflater;<br style="line-height:25px"> importandroid.view.View;<br style="line-height:25px"> importandroid.widget.ListView;<br style="line-height:25px"> importandroid.view.ViewGroup;<br style="line-height:25px"> importandroid.widget.ArrayAdapter;<br style="line-height:25px"> importandroid.widget.CursorAdapter;<br style="line-height:25px"> importandroid.widget.ResourceCursorAdapter;<br style="line-height:25px"> importandroid.widget.TextView;<br style="line-height:25px"> importandroid.provider.ContactsContract.Contacts;<br style="line-height:25px"> importandroid.provider.ContactsContract.RawContacts;<br style="line-height:25px"> importandroid.view.View.OnClickListener;<br style="line-height:25px"> importandroid.widget.Button;<br style="line-height:25px"> importandroid.util.Log;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#ff6600; line-height:25px">HelloCursor</span>extendsListActivity{<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">privatestatic</span><span style="color:#3366ff; line-height:25px">String[]PROJECTION=newString[]{Contacts._ID,</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Contacts.DISPLAY_NAME};</span><br style="line-height:25px"><br style="line-height:25px"><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:#808080; line-height:25px">//MyCursorAdapteradapter=newMyCursorAdapter(this,R.layout.list_row,c);</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">MyResourceCursorAdapteradapter=newMyResourceCursorAdapter(this,R.layout.list_row,<br style="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><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">cmd=CMD_ADD;</span><br style="line-height:25px"><span style="color:#3366ff; 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">Buttonbutton2=(Button)findViewById(R.id.Button02);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">OnClickListenerlistener2=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><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">cmd=CMD_DELETE_ALL;</span><br style="line-height:25px"><span style="color:#3366ff; 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">button2.setOnClickListener(listener2);</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">intcmd=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">finalintCMD_ADD=1;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">finalintCMD_DELETE_ALL=2;</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:#ff00ff; line-height:25px">ContactThread</span><span style="color:#3366ff; line-height:25px">implementsRunnable{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidrun(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">//HelloCursor.this.getListView().invalidateViews();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(cmd==CMD_ADD)</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:#0000ff; line-height:25px">addContacts();</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">elseif(cmd==CMD_DELETE_ALL)</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:#0000ff; line-height:25px">dealAllContacts();</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">cnt++;</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><span style="color:#993300; line-height:25px">void</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">addContacts()</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">intnStringLength=mStrings.length;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">intrandomNumber=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ContentValuesnewValues=newContentValues();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">StringtempString=null;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">randomNumber=(int)(Math.random()%10);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">for(inti=0;i<nStringLength;i++){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">tempString=mStrings</span><wbr style="line-height:25px"><span style="color:#3366ff; line-height:25px">+cnt+randomNumber;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">newValues.put(Contacts.DISPLAY_NAME,tempString);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">getContentResolver().insert(RawContacts.CONTENT_URI,newValues);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">newValues.clear();</span><br style="line-height:25px"><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">Log.i("hubin","add");</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">void</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">dealAllContacts()</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">getContentResolver().delete(RawContacts.CONTENT_URI,null,null);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i("hubin","delete");</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">ContactThreadcontactDealer=newContactThread();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">void</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; 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">mHandler.post(contactDealer);</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:#993300; line-height:25px">class</span><span style="color:#ff6600; line-height:25px">MyResourceCursorAdapter</span>extendsResourceCursorAdapter{<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"></span><span style="color:#ff6600; line-height:25px">MyResourceCursorAdapter</span><span style="color:#3366ff; line-height:25px">(Contextcontext,intresource,Cursorcursor){<br style="line-height:25px"> super(context,resource,cursor);<br style="line-height:25px"> }<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">View</span><span style="color:#ff6600; line-height:25px">newView</span><span style="color:#3366ff; line-height:25px">(Contextcontext,Cursorcursor,ViewGroupparent){<br style="line-height:25px"> returnsuper.newView(context,cursor,parent);<br style="line-height:25px"> }<br style="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:#ff6600; line-height:25px">bindView</span><span style="color:#3366ff; line-height:25px">(Viewview,Contextcontext,Cursorcursor){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">Log.i("hubin","bind"+view);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">TextViewnameView=(TextView)view;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">//Setthename</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">nameView.setText(cursor</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">.getString(cursor.getColumnIndex("DISPLAY_NAME")));</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><br style="line-height:25px"> }<br style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#ff6600; line-height:25px">MyCursorAdapter</span>extendsCursorAdapter{<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Contextcontext=null;<br style="line-height:25px"> intviewResId;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">MyCursorAdapter</span><span style="color:#3366ff; line-height:25px">(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:#ff6600; line-height:25px">newView</span><span style="color:#3366ff; line-height:25px">(Contextcontext,Cursorcursor,ViewGroupparent){<br style="line-height:25px"><br style="line-height:25px"> TextViewview=null;<br style="line-height:25px"> LayoutInflatervi=null;<br style="line-height:25px"> vi=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);<br style="line-height:25px"> view=(TextView)vi.inflate(viewResId,parent,false);<br style="line-height:25px"></span><span style="color:#808080; 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">returnview;</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:#ff6600; 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"></span><span style="color:#808080; line-height:25px">//Setthename</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">nameView.setText(cursor</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">.getString(cursor.getColumnIndex("DISPLAY_NAME")));</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><br style="line-height:25px"> }</wbr></wbr>
ResourceCursorAdapter
extendsCursorAdapter
java.lang.Object
android.widget.BaseAdapter
android.widget.CursorAdapter
android.widget.ResourceCursorAdapter
ClassOverview
AneasyadapterthatcreatesviewsdefinedinanXMLfile.
YoucanspecifytheXMLfilethatdefinestheappearanceoftheviews.
<wbr style="line-height:25px">ResourceCursorAdapter</wbr> 是虚类,它继承于 CursorAdapte r,它实现了 CursorAdapter 的 newView 方法
| <nobr style="line-height:21px"><a rel="nofollow" href="http://developer.android.com/reference/android/view/View.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">View</a></nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/widget/ResourceCursorAdapter.html#newView(android.content.Context,%20android.database.Cursor,%20android.view.ViewGroup)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">newView</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/content/Context.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Context</a>context,<a rel="nofollow" href="http://developer.android.com/reference/android/database/Cursor.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Cursor</a>cursor,<a rel="nofollow" href="http://developer.android.com/reference/android/view/ViewGroup.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ViewGroup</a>parent)</nobr>
Inflates view(s) from the specified XML file.
|
| Public Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| <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/widget/CursorAdapter.html#bindView(android.view.View,%20android.content.Context,%20android.database.Cursor)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">bindView</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/view/View.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">View</a>view,<a rel="nofollow" href="http://developer.android.com/reference/android/content/Context.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Context</a>context,<a rel="nofollow" href="http://developer.android.com/reference/android/database/Cursor.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Cursor</a>cursor)</nobr>
Bind an existing view to the data pointed to by cursor
| ||||||||||
本博客介绍ResourceCursorAdapter类的概述、方法及其实现,并通过示例展示其在Android开发中的应用。
1922

被折叠的 条评论
为什么被折叠?



