ArrayAdapter的定义
各种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}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。