各种Adapter的用法(适配器)

*同样是一个ListView,可以用不同的Adapter让它显示出来,比如说最常用的ArrayAdapter, SimpleAdapter,BaseAdapter.

android.widget.ArrayAdapter<string>AconcreteBaseAdapterthatisbackedbyanarrayofarbitraryobjects.BydefaultthisclassexpectsthattheprovidedresourceidreferencesasingleTextView.Ifyouwanttouseamorecomplexlayout,usetheconstructorsthatalsotakesafieldid.ThatfieldidshouldreferenceaTextViewinthelargerlayoutresource.HowevertheTextViewisreferenced,itwillbefilledwiththetoString()ofeachobjectinthearray.Youcanaddlistsorarraysofcustomobjects.OverridethetoString()methodofyourobjectstodeterminewhattextwillbedisplayedfortheiteminthelist.TousesomethingotherthanTextViewsforthearraydisplay,forinstance,ImageViews,ortohavesomeofdatabesidestoString()resultsfilltheviews,overridegetView(int,View,ViewGroup)toreturnthetypeofviewyouwant.



如何定义ArrayAdapter


错误实例:

packagecom.example.testandroidproject;importandroid.os.Bundle;importandroid.support.v7.app.ActionBarActivity;importandroid.widget.ArrayAdapter;publicclassMainActivityextendsActionBarActivity{privateString[]ganlist=newString[]{"孙悟空","猪八戒","沙和尚"};@OverrideprotectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);privateArrayAdapter<String>arrayAdapter=newArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,ganlist);//这样定义ArrayAdapter会出现红色错误}}

*****************************************************************************************************

Multiple markers at this line
- Illegal modifier for parameter arrayAdapter; only final is permitted
- Line breakpoint:MainActivity [line: 15] - onCreate(Bundle)

*****************************************************************************************************

正确实例:

packagecom.example.testandroidproject;importandroid.os.Bundle;importandroid.support.v7.app.ActionBarActivity;importandroid.widget.ArrayAdapter;publicclassMainActivityextendsActionBarActivity{privateString[]ganlist=newString[]{"孙悟空","猪八戒","沙和尚"};@OverrideprotectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);ArrayAdapter<String>arrayAdapter=newArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,ganlist);//去掉private}}