Android-- ArrayAdapter用法舉例,androidadapter

來源:互聯網
上載者:User

Android-- ArrayAdapter用法舉例(轉載),androidadapter

      近期很多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檔案來實現自訂顯示效果。

Java代碼  
  1. public class IconListAdapter extends ArrayAdapter<IconListAdapter.IconListItem> {  
  2.     protected LayoutInflater mInflater;  
  3.     private static final int mResource = R.layout.icon_list_item; //xml布局檔案  
  4.   
  5.     public IconListAdapter(Context context,  
  6.             List<IconListItem> items) {  
  7.         super(context, mResource, items);  
  8.         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  9.     }  
  10.   
  11.     @Override  
  12.     public View getView(int position, View convertView, ViewGroup parent) {  
  13.         TextView text;  
  14.         ImageView image;  
  15.   
  16.         View view;  
  17.         if (convertView == null) {  
  18.             view = mInflater.inflate(mResource, parent, false);  
  19.         } else {  
  20.             view = convertView;  
  21.         }  
  22.   
  23.         text = (TextView) view.findViewById(R.id.text1);  
  24.         text.setText(getItem(position).getTitle());  
  25.   
  26.         image = (ImageView) view.findViewById(R.id.icon);  //可以使用上文說的三種方法,直接用TextView類的setCompoundDrawables等方法綁定表徵圖顯示  
  27.         image.setImageResource(getItem(position).getResource());  
  28.   
  29.         return view;  
  30.     }  
  31.   
  32.     public static class IconListItem {  //每條顯示的構造方法  
  33.         private final String mTitle;  
  34.         private final int mResource;  
  35.   
  36.         public IconListItem(String title, int resource) {  
  37.             mResource = resource;  
  38.             mTitle = title;  
  39.         }  
  40.   
  41.         public String getTitle() {  
  42.             return mTitle;  
  43.         }  
  44.   
  45.         public int getResource() {  
  46.             return mResource;  
  47.         }  
  48.     }  
  49. }  

 

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

Java代碼  
  1. void  add(T object)  //添加一個對象到本ArrayAdapter  
  2.   
  3. void  clear()  //清除所有元素  
  4.   
  5. static ArrayAdapter<CharSequence>  createFromResource(Context context, int textArrayResId, int textViewResId)  //從layout資源構造arrayadapter  
  6.   
  7. Context  getContext()  //擷取執行個體  
  8.   
  9. int  getCount()   
  10.   
  11. View  getDropDownView(int position, View convertView, ViewGroup parent)  //擷取drop down的popup風格選擇條目的內容,參數1是位置,參數2可以通過強制轉換直接擷取本條的內容  
  12.   
  13. Filter  getFilter() //使用正則過濾資料   
  14.   
  15. T  getItem(int position)  //擷取單條內容  
  16.   
  17. long  getItemId(int position)    
  18.   
  19. int  getPosition(T item) //通過內容擷取是某條  
  20.   
  21. View  getView(int position, View convertView, ViewGroup parent)   
  22.   
  23. void  insert(T object, int index)  //插入新條目到數組的index位置  
  24.   
  25. void  notifyDataSetChanged()  //通知數據變化了,告訴綁定Adapter的widget來更新UI  
  26.   
  27. void  remove(T object)  //移出一條從數組,這裡並沒有指定位置  
  28.   
  29. void  setDropDownViewResource(int resource)  //設定dropdown的layout風格  
  30. Sets the layout resource to create the drop down views.  
  31.   
  32. void  setNotifyOnChange(boolean notifyOnChange)  //本條是arrayadapter最強大的功能,android123強烈推薦處理大資料時使用該方法,可以降低ui的處理量,重新整理ui可以更快速,主要可以停止對  
  33. (add(T), insert(T, int), remove(T), clear() 的操作,當然可以通過 notifyDataSetChanged(). 或 setNotifyOnChange(true) 通知變化  
  34.   
  35. 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.