Android– ArrayAdapter用法舉例

來源:互聯網
上載者:User

近期很多Android開發人員來函表示對ArrayAdapter和BaseAdapter的區別不是很清楚,這裡Android123簡單說下他們的關係和用處,ArrayAdapter是從BaseAdapter派生出來的,具備BaseAdapter的所有功能,但ArrayAdapter更為強大,它執行個體化時可以直接使用泛型構造,我們在Android SDK中可以看到android.widget.ArrayAdapter<T>的字樣,當然也可以使用 ArrayAdapter(Context context, int textViewResourceId) 第二個參數直接綁定一個layout,下文的例子我們使用Java泛型執行個體化。

通過Adapter我們構造一個支援icon的item,下面我們在getView中使用的是imageView顯示圖片,當然android123提示大家其實TextView也可以直接綁定一個drawable對象顯示的,void  setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) 或void  setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) 和void  setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) 即可,其中第二種的int類型指定的資源id,方位則是textview什麼位置顯示drawable對象

說了這麼多ArrayAdapater一起看個例子,來執行個體化ArrayAdapter吧,我們可以修改Res/layout/icon_list_item.xml檔案來實現自訂顯示效果。

public class IconListAdapter extends ArrayAdapter<IconListAdapter.IconListItem> {<br /> protected LayoutInflater mInflater;<br /> private static final int mResource = R.layout.icon_list_item; //xml布局檔案</p><p> public IconListAdapter(Context context,<br /> List<IconListItem> items) {<br /> super(context, mResource, items);<br /> mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);<br /> }</p><p> @Override<br /> public View getView(int position, View convertView, ViewGroup parent) {<br /> TextView text;<br /> ImageView image;</p><p> View view;<br /> if (convertView == null) {<br /> view = mInflater.inflate(mResource, parent, false);<br /> } else {<br /> view = convertView;<br /> }</p><p> text = (TextView) view.findViewById(R.id.text1);<br /> text.setText(getItem(position).getTitle());</p><p> image = (ImageView) view.findViewById(R.id.icon); //可以使用上文說的三種方法,直接用TextView類的setCompoundDrawables等方法綁定表徵圖顯示<br /> image.setImageResource(getItem(position).getResource());</p><p> return view;<br /> }</p><p> public static class IconListItem { //每條顯示的構造方法<br /> private final String mTitle;<br /> private final int mResource;</p><p> public IconListItem(String title, int resource) {<br /> mResource = resource;<br /> mTitle = title;<br /> }</p><p> public String getTitle() {<br /> return mTitle;<br /> }</p><p> public int getResource() {<br /> return mResource;<br /> }<br /> }<br />}

當然對於ArrayAdapter到底比BaseAdapter先進到哪裡呢?  從名稱來看Array我們可以聯絡到數組的很多操作,沒錯Android123給大家列出本類所有成員方法實用的處理方式,比如:.

void add(T object) //添加一個對象到本ArrayAdapter</p><p>void clear() //清除所有元素</p><p>static ArrayAdapter<CharSequence> createFromResource(Context context, int textArrayResId, int textViewResId) //從layout資源構造arrayadapter</p><p>Context getContext() //擷取執行個體</p><p>int getCount() </p><p>View getDropDownView(int position, View convertView, ViewGroup parent) //擷取drop down的popup風格選擇條目的內容,參數1是位置,參數2可以通過強制轉換直接擷取本條的內容</p><p>Filter getFilter() //使用正則過濾資料 </p><p>T getItem(int position) //擷取單條內容</p><p>long getItemId(int position) </p><p>int getPosition(T item) //通過內容擷取是某條</p><p>View getView(int position, View convertView, ViewGroup parent) </p><p>void insert(T object, int index) //插入新條目到數組的index位置</p><p>void notifyDataSetChanged() //通知數據變化了,告訴綁定Adapter的widget來更新UI</p><p>void remove(T object) //移出一條從數組,這裡並沒有指定位置</p><p>void setDropDownViewResource(int resource) //設定dropdown的layout風格<br />Sets the layout resource to create the drop down views.</p><p>void setNotifyOnChange(boolean notifyOnChange) //本條是arrayadapter最強大的功能,android123強烈推薦處理大資料時使用該方法,可以降低ui的處理量,重新整理ui可以更快速,主要可以停止對<br />(add(T), insert(T, int), remove(T), clear() 的操作,當然可以通過 notifyDataSetChanged(). 或 setNotifyOnChange(true) 通知變化</p><p>void sort(Comparator<? super T> comparator) //這裡是android開發網經常用的排序,使用arrayadapter可以直接排序,十分方便  

所以最終android123推薦大家什麼情況使用arrayadapter,什麼時候使用baseadapter。當數量較多,比如超過100條或頻繁動態增減時使用arrayadapter可以方便控制ui,通過setNotifyOnChanage方法,如果比較簡單僅僅呈現直接從 baseadapter更節省資源。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.