zoukankan      html  css  js  c++  java
  • Android-- ArrayAdapter用法举例(转载)

          近期很多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更节省资源。

  • 相关阅读:
    C#往SQLServer中插入大数据
    (5.1.2)引擎管理——启动参数与单用户模式启动
    (5.1.1)引擎管理——包含数据库解决迁移造成孤立用户问题
    【转】mysql的group_concat函数,默认最大长度是1024
    【转】【mysql面试】https://blog.csdn.net/hanfazy/article/details/14520437
    【基本优化实践】【1.2】索引优化——重建索引、查看堆表、查看索引使用情况、查看索引碎片率
    【基本优化实践】【1.1】IO优化——把文件迁移到不同物理磁盘
    sql server优化方向?
    mysql中的事务隔离级别与锁
    Stanford大学机器学习公开课(三):局部加权回归、最小二乘的概率解释、逻辑回归、感知器算法
  • 原文地址:https://www.cnblogs.com/xiaochao1234/p/4201662.html
Copyright © 2011-2022 走看看