ArrayAdapter

ArrayAdapter<T>
继承于BaseAdapter
ArrayAdapter(数组适配器)的作用就是一个数组和listview之间的桥梁,
java.lang.Object
android.widget.BaseAdapter
android.widget.ArrayAdapter<T>
IthasnoSubclasses
publicclassArrayAdapterextendsBaseAdapterimplementsFilterable

ClassOverview

AListAdapterthatmanagesaListViewbackedbyanarrayofarbitraryobjects.
BydefaultthisclassexpectsthattheprovidedresourceidreferencesasingleTextView.
Ifyouwanttouseamorecomplexlayout,usetheconstructorsthatalsotakesafieldid.
ThatfieldidshouldreferenceaTextViewinthelargerlayoutresource.
HowevertheTextViewisreferenced,itwillbefilledwiththetoString()ofeachobjectinthearray.
Youcanaddlistsorarraysofcustomobjects.OverridethetoString()methodofyourobjectstodetermine
whattextwillbedisplayedfortheiteminthelist.
TousesomethingotherthanTextViewsforthearraydisplay,
forinstance,ImageViews,ortohavesomeofdatabesidestoString()resultsfilltheviews,
overridegetView(int,View,ViewGroup)toreturnthetypeofviewyouwant.

ArrayAdapter可以将数组里边定义的数据一一对应的显示在Listview里边。
ListView的每个TextView里边显示的内容就是数组里边的对象调用toString()方法后生成的字符串。
例子1:
ListViewlistview=newListView(this);
//构造一个listview对象
String[]data={“google”,”amazon”,”facebook”};
//构造一个数组对象,也就是数据
listview.setAdapter(newArrayAdapter(this,android.R.layout.simple_list_item_single_choice,data));
//构造一个array适配器,然后listview对象通过setAdapter方法调用适配器来和自己绑定数据
list.setItemsCanFocus(true);
list.setChoiceMode(listview.CHOICE_MODE_MULTIPLE);
setContentView(listview);
注:
ArrayAdapter和ListView的更多使用参照《[url=javascript:;][img]../../../../ac/b.gif[/img][/url][url=javascript:;]ListActivity和ListView》<wbr style="line-height:25px">[/url]<br style="line-height:25px"><span style="line-height:25px">例子2:</span><br style="line-height:25px"><br style="line-height:25px"> String[]textData={<br style="line-height:25px"> "Googlemail",<br style="line-height:25px"> "Messaging",<br style="line-height:25px"> "PicSay"<br style="line-height:25px"> };<br style="line-height:25px"> int[]imgData={<br style="line-height:25px"> R.drawable.alertdialogsample1xml_pink,<br style="line-height:25px"> R.drawable.alertdialogsample1xml_pink,<br style="line-height:25px"> R.drawable.alertdialogsample1xml_pink<br style="line-height:25px"> };<br style="line-height:25px"><br style="line-height:25px"><br style="line-height:25px"> ArrayList&lt;AlertDialogSample10Item&gt;al=newArrayList&lt;AlertDialogSample10Item&gt;();<br style="line-height:25px"><br style="line-height:25px"> for(inti=0;i&lt;3;i++)<br style="line-height:25px"> al.add(newAlertDialogSample10Item(imgData<wbr style="line-height:25px">,textData<wbr style="line-height:25px">));<br style="line-height:25px"><br style="line-height:25px"> AlertDialogSample10Adapterlistadapter10=newAlertDialogSample10Adapter(this,R.layout.alertdialogsample1xml_sample10_row_item,al);<br style="line-height:25px"><br style="line-height:25px"> AlertDialog.Builderalertdialog10=newAlertDialog.Builder(AlertDialogSample1Xml.this);<br style="line-height:25px"><br style="line-height:25px"> alertdialog10.setTitle(R.string.alertdialogsample1xml_sharepicturevia);<br style="line-height:25px"> alertdialog10.setIcon(R.drawable.alertdialogsample1xml_title_icon_photobucket);<br style="line-height:25px"><br style="line-height:25px"> /*<br style="line-height:25px"> *setcustomlistapdatertoalertdialogbody<br style="line-height:25px"> */<br style="line-height:25px"> alertdialog10.setAdapter(listadapter10,newDialogInterface.OnClickListener(){<br style="line-height:25px"> publicvoidonClick(DialogInterfacedialog,intwhich){<br style="line-height:25px"> Toast.makeText(context,(which+1)+"itemclick",Toast.LENGTH_SHORT).show();<br style="line-height:25px"> }<br style="line-height:25px"> });<br style="line-height:25px"><br style="line-height:25px"> returnalertdialog10.create();<br style="line-height:25px"><br style="line-height:25px"> classAlertDialogSample10Item{<br style="line-height:25px"> privateIntegermAppIcon;<br style="line-height:25px"> privateStringmAppName;<br style="line-height:25px"><br style="line-height:25px"> publicAlertDialogSample10Item(Integerappicon,Stringappname){<br style="line-height:25px"> this.mAppIcon=appicon;<br style="line-height:25px"> this.mAppName=appname;<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> publicIntegergetAppIcon(){<br style="line-height:25px"> returnmAppIcon;<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> publicStringgetAppName(){<br style="line-height:25px"> returnmAppName;<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"><span style="color:#993300; line-height:25px">privateclass</span>AlertDialogSample8AdapterextendsArrayAdapter&lt;AlertDialogSample8Item&gt;{<br style="line-height:25px"> privateArrayList&lt;AlertDialogSample8Item&gt;items;<br style="line-height:25px"><br style="line-height:25px"> publicAlertDialogSample8Adapter(Contextcontext,inttextViewResourceId,ArrayList&lt;AlertDialogSample8Item&gt;items){<br style="line-height:25px"> super(context,textViewResourceId,items);<br style="line-height:25px"> this.items=items;<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"><br style="line-height:25px"> @Override<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicView</span><span style="line-height:25px">getView</span>(intposition,ViewconvertView,ViewGroupparent){<br style="line-height:25px"> Viewv=convertView;<br style="line-height:25px"> if(v==null){<br style="line-height:25px"> LayoutInflatervi=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);<br style="line-height:25px"> v=vi.inflate(R.layout.alertdialogsample1xml_sample8_row,null);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">AlertDialogSample8Itemti=items.get(position);</span><br style="line-height:25px"> if(ti!=null){<br style="line-height:25px"> ImageViewappicon=(ImageView)v.findViewById(R.id.appicon);<br style="line-height:25px"> TextViewappname=(TextView)v.findViewById(R.id.apptext);<br style="line-height:25px"><br style="line-height:25px"> if(appicon!=null){<br style="line-height:25px"> appicon.setImageResource(ti.getAppIcon());<br style="line-height:25px"> }<br style="line-height:25px"> if(appname!=null){<br style="line-height:25px"> appname.setText(ti.getAppName());<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> returnv;<br style="line-height:25px"> }<br style="line-height:25px"> }</wbr></wbr></wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值