ArrayAdapter跟BaseAdapter的区别

ArrayAdapter和BaseAdapter的区别
近期很多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文件来实现自定义显示效果。

view plaincopy to clipboardprint?
01.public class IconListAdapter extends ArrayAdapter<IconListAdapter.IconListItem> {  
02.    protected LayoutInflater mInflater;  
03.    private static final int mResource = R.layout.icon_list_item; //xml布局文件  
04. 
05.    public IconListAdapter(Context context,  
06.            List<IconListItem> items) {  
07.        super(context, mResource, items);  
08.        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
09.    }  
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.} 
public class IconListAdapter extends ArrayAdapter<IconListAdapter.IconListItem> {
    protected LayoutInflater mInflater;
    private static final int mResource = R.layout.icon_list_item; //xml布局文件

    public IconListAdapter(Context context,
            List<IconListItem> items) {
        super(context, mResource, items);
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView text;
        ImageView image;

        View view;
        if (convertView == null) {
            view = mInflater.inflate(mResource, parent, false);
        } else {
            view = convertView;
        }

        text = (TextView) view.findViewById(R.id.text1);
        text.setText(getItem(position).getTitle());

        image = (ImageView) view.findViewById(R.id.icon);  //可以使用上文说的三种方法,直接用TextView类的setCompoundDrawables等方法绑定图标显示
        image.setImageResource(getItem(position).getResource());

        return view;
    }

    public static class IconListItem {  //每条显示的构造方法
        private final String mTitle;
        private final int mResource;

        public IconListItem(String title, int resource) {
            mResource = resource;
            mTitle = title;
        }

        public String getTitle() {
            return mTitle;
        }

        public int getResource() {
            return mResource;
        }
    }
}

当然对于ArrayAdapter到底比BaseAdapter先进到哪里呢?  从名称来看Array我们可以联系到数组的很多操作,没错Android123给大家列出本类所有成员方法实用的处理方式,比如:.

view plaincopy to clipboardprint?
01.void  add(T object)  //添加一个对象到本ArrayAdapter  
02. 
03.void  clear()  //清除所有元素  
04. 
05.static ArrayAdapter<CharSequence>  createFromResource(Context context, int textArrayResId, int textViewResId)  //从layout资源构造arrayadapter  
06. 
07.Context  getContext()  //获取实例  
08. 
09.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可以直接排序,十分方便 
void  add(T object)  //添加一个对象到本ArrayAdapter

void  clear()  //清除所有元素

static ArrayAdapter<CharSequence>  createFromResource(Context context, int textArrayResId, int textViewResId)  //从layout资源构造arrayadapter

Context  getContext()  //获取实例

int  getCount()

View  getDropDownView(int position, View convertView, ViewGroup parent)  //获取drop down的popup风格选择条目的内容,参数1是位置,参数2可以通过强制转换直接获取本条的内容

Filter  getFilter() //使用正则过滤数据

T  getItem(int position)  //获取单条内容

long  getItemId(int position) 

int  getPosition(T item) //通过内容获取是某条

View  getView(int position, View convertView, ViewGroup parent)

void  insert(T object, int index)  //插入新条目到数组的index位置

void  notifyDataSetChanged()  //通知数据变化了,告诉绑定Adapter的widget来更新UI

void  remove(T object)  //移出一条从数组,这里并没有指定位置

void  setDropDownViewResource(int resource)  //设置dropdown的layout风格
Sets the layout resource to create the drop down views.

void  setNotifyOnChange(boolean notifyOnChange)  //本条是arrayadapter最强大的功能,android123强烈推荐处理大数据时使用该方法,可以降低ui的处理量,刷新ui可以更快速,主要可以停止对
(add(T), insert(T, int), remove(T), clear() 的操作,当然可以通过 notifyDataSetChanged(). 或 setNotifyOnChange(true) 通知变化

void  sort(Comparator<? super T> comparator)  //这里是android开发网经常用的排序,使用arrayadapter可以直接排序,十分方便 

所以最终android123推荐大家什么情况使用arrayadapter,什么时候使用baseadapter。当数量较多,比如超过100条或频繁动态增减时使用arrayadapter可以方便控制ui,通过setNotifyOnChanage方法,如果比较简单仅仅呈现直接从 baseadapter更节省资源。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/pilou5400/archive/2010/12/08/6062837.aspx