android ArrayAdapter详解

来源:互联网 发布:网络廷长器 编辑:程序博客网 时间:2024/05/16 16:12
ArrayAdapter是继承BaseAdapter而来,还实现了Filterable接口,Filterable接口顾名思义,就是个过滤器,比如电话簿的查找功能,可以通过这个过滤器来得到查找结果,既然有过滤功能,那过滤器得到的数据,就应该被适配器获取并更新,所以ArrayAdapter还有类似数组的insert、add、addall、remove、clear等功能,可以对ArrayAdapter的数据进行插入、增加、移除、清除,比如通过过滤器进行了过滤以后,需要对ArrayAdapter的数据进行更新,那么就先把ArrayAdapter的数据进行清空(clear),再进行加载工作(add 、addall等),以下是ArrayAdapter.java的源码

package android.widget;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class ArrayAdapter<T> extends BaseAdapter implements Filterable {
    
    private List<T> mObjects;  //用于保存适配器所获取的数据

    
    private final Object mLock = new Object(); //一个锁,同步锁

   
    private int mResource;   //资源的id,一般是layout文件的id

    
    private int mDropDownResource;  //是mResource的一个拷贝


    
    private int mFieldId = 0;              //一般指layout文件里的一个id

    
    private boolean mNotifyOnChange = true;

    private Context mContext;

    
    private ArrayList<T> mOriginalValues;   //是mObjects的一个拷贝,主要用于处理过滤器的数据

    private ArrayFilter mFilter;        //这就是传说中的过滤器了,查询功能得靠它来实现

    private LayoutInflater mInflater;   //一个LayoutInflater,用于导入一个layout


    
  /*下面都是不同给构造函数,没啥好说的,重点在于init(……)函数*/
    public ArrayAdapter(Context context, int textViewResourceId) {
        init(context, textViewResourceId, 0, new ArrayList<T>());
    }

    
    public ArrayAdapter(Context context, int resource, int textViewResourceId) {
        init(context, resource, textViewResourceId, new ArrayList<T>());
    }

   
    public ArrayAdapter(Context context, int textViewResourceId, T[] objects) {
        init(context, textViewResourceId, 0, Arrays.asList(objects));
    }

    
    public ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) {
        init(context, resource, textViewResourceId, Arrays.asList(objects));
    }

    
    public ArrayAdapter(Context context, int textViewResourceId, List<T> objects) {
        init(context, textViewResourceId, 0, objects);
    }

    
    public ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects) {
        init(context, resource, textViewResourceId, objects);
    }

    
    /*以下就是对适配器的数据进行类似数组的操作处理,插入、增加、清除、移除*/
    public void add(T object) {
        synchronized (mLock) {
            if (mOriginalValues != null) {
                mOriginalValues.add(object);
            } else {
                mObjects.add(object);
            }
        }
        if (mNotifyOnChange) notifyDataSetChanged();
    }

    
    public void addAll(Collection<? extends T> collection) {
        synchronized (mLock) {
            if (mOriginalValues != null) {
                mOriginalValues.addAll(collection);
            } else {
                mObjects.addAll(collection);
            }
        }
        if (mNotifyOnChange) notifyDataSetChanged();
    }

    
    public void addAll(T ... items) {
        synchronized (mLock) {
            if (mOriginalValues != null) {
                Collections.addAll(mOriginalValues, items);
            } else {
                Collections.addAll(mObjects, items);
            }
        }
        if (mNotifyOnChange) notifyDataSetChanged();
    }

    
    public void insert(T object, int index) {
        synchronized (mLock) {
            if (mOriginalValues != null) {
                mOriginalValues.add(index, object);
            } else {
                mObjects.add(index, object);
            }
        }
        if (mNotifyOnChange) notifyDataSetChanged();
    }

    
    public void remove(T object) {
        synchronized (mLock) {
            if (mOriginalValues != null) {
                mOriginalValues.remove(object);
            } else {
                mObjects.remove(object);
            }
        }
        if (mNotifyOnChange) notifyDataSetChanged();
    }

    public void clear() {
        synchronized (mLock) {
            if (mOriginalValues != null) {
                mOriginalValues.clear();
            } else {
                mObjects.clear();
            }
        }
        if (mNotifyOnChange) notifyDataSetChanged();
    }

    
    public void sort(Comparator<? super T> comparator) {
        synchronized (mLock) {
            if (mOriginalValues != null) {
                Collections.sort(mOriginalValues, comparator);
            } else {
                Collections.sort(mObjects, comparator);
            }
        }
        if (mNotifyOnChange) notifyDataSetChanged();
    }

    
    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        mNotifyOnChange = true;
    }

    
    public void setNotifyOnChange(boolean notifyOnChange) {
        mNotifyOnChange = notifyOnChange;
    }

   /*这是其实就是对一些变量的初始化,上下文
mContext,LayoutInflater,layout布局文件的id,还是适配器数据的原始数据mObjects,还有layout文件里的某个要显示的textview---mFieldId */
    private void init(Context context, int resource, int textViewResourceId, List<T> objects) {
        mContext = context;
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mResource = mDropDownResource = resource;
        mObjects = objects;
        mFieldId = textViewResourceId;
    }


    /*以下都是从BaseAdapter继承过来的一些函数*/
    public Context getContext() {
        return mContext;
    }

    
    public int getCount() {
        return mObjects.size();
    }

    
    public T getItem(int position) {
        return mObjects.get(position);
    }

    
    public int getPosition(T item) {
        return mObjects.indexOf(item);
    }

   
    public long getItemId(int position) {
        return position;
    }

    
    public View getView(int position, View convertView, ViewGroup parent) {
        return createViewFromResource(position, convertView, parent, mResource);
    }

   
   /*在这里要注意一点,别看这个类用到泛型类型,但是还是有限制的,就是ArrayAdapter<T>中<T>必须是TextView类,否则会出错*/
    private View createViewFromResource(int position, View convertView, ViewGroup parent,
            int resource) {
        View view;
        TextView text;

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

        try {
            if (mFieldId == 0) {
                //  If no custom field is assigned, assume the whole resource is a TextView
                text = (TextView) view;
            } else {
                //  Otherwise, find the TextView field within the layout
                text = (TextView) view.findViewById(mFieldId);
            }
        } catch (ClassCastException e) {
           /*ArrayAdapter<T>中<T>必须是TextView类,否则会出错*/
            Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
            throw new IllegalStateException(
                    "ArrayAdapter requires the resource ID to be a TextView", e);
        }

        T item = getItem(position);
        if (item instanceof CharSequence) {
            text.setText((CharSequence)item);
        } else {
            text.setText(item.toString());
        }

        return view;
    }


    
    public void setDropDownViewResource(int resource) {
        this.mDropDownResource = resource;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return createViewFromResource(position, convertView, parent, mDropDownResource);
    }

   
    public static ArrayAdapter<CharSequence> createFromResource(Context context,
            int textArrayResId, int textViewResId) {
        CharSequence[] strings = context.getResources().getTextArray(textArrayResId);
        return new ArrayAdapter<CharSequence>(context, textViewResId, strings);
    }

   
   /*这个函数很有用,可以返回一个filter,此时这个函数又把适配器的原始数据刷新了一遍,得到了一个Filter实例之后,
    就可以调用有参数的filter(……)函数来查询用户关心的数据了,具体下面会分析*/
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ArrayFilter();
        }
        return mFilter;
    }


   
    private class ArrayFilter extends Filter {
    
       /*这个方法就是在用户进行查询数据所要用到的,那么是怎么用的呢,就是前面用getFilter()返回一个Filter的实例,得到的实例
         来执行方法filter(……)来查询用户关心的数据,filter(……)的形参,就是一些字符串*/
        @Override
        protected FilterResults 
performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();

            if (mOriginalValues == null) {
                synchronized (mLock) {
                    mOriginalValues = new ArrayList<T>(mObjects);
                }
            }

            if (prefix == null || prefix.length() == 0) {
                ArrayList<T> list;
                synchronized (mLock) {
                    list = new ArrayList<T>(mOriginalValues);
                }
                results.values = list;
                results.count = list.size();
            } else {
                String prefixString = prefix.toString().toLowerCase();

                ArrayList<T> values;
                synchronized (mLock) {
                    values = new ArrayList<T>(mOriginalValues);
                }

                final int count = values.size();
                final ArrayList<T> newValues = new ArrayList<T>();

                for (int i = 0; i < count; i++) {
                    final T value = values.get(i);
                    final String valueText = value.toString().toLowerCase();

                    // First match against the whole, non-splitted value
                    if (valueText.startsWith(prefixString)) {
                        newValues.add(value);
                    } else {
                        final String[] words = valueText.split(" ");
                        final int wordCount = words.length;

                        // Start at index 0, in case valueText starts with space(s)
                        for (int k = 0; k < wordCount; k++) {
                            if (words[k].startsWith(prefixString)) {
                                newValues.add(value);
                                break;
                            }
                        }
                    }
                }

                results.values = newValues;
                results.count = newValues.size();
            }

            return results;
        }


        
       /*函数performFiltering(CharSequence prefix) 顾名思义就是执行过滤功能,当执行new ArrayFilter()时,此函数就是执行
         这就是为什么上面的执行方法getFilter() 时会适配器的原始数据刷新了一遍的原因,因为执行performFiltering(CharSequence prefix)         了,具体的会在下面的类Filter中讲到*/
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            //noinspection unchecked
            mObjects = (List<T>) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }
}


以上,就是适配器ArrayAdapter的全部代码,总的来说,它就是一个有查询功能的BaseAdapter,而之所以具备了查询功能,是因为它实现了Filter接口,具有过滤功能,下面来看一下Filter接口的构造,代码如下:

package android.widget;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;


public abstract class Filter {
    private static final String LOG_TAG = "Filter";
    
    private static final String THREAD_NAME = "Filter";
    private static final int FILTER_TOKEN = 0xD0D0F00D;
    private static final int FINISH_TOKEN = 0xDEADBEEF;

    private Handler mThreadHandler;
    private Handler mResultHandler;

    private Delayer mDelayer;

    private final Object mLock = new Object();

    
   /*看到这里了,这个就是构造函数,上面调用getFilter()的时候,这里就会得到执行,而具体执行的是mResultHandler = new ResultsHandler();
     具体的转到ResultsHandler里来看*/
    public Filter() {
        mResultHandler = new ResultsHandler();
    }


    
    /*就是一个发消息的一个延时时间*/
    public void setDelayer(Delayer delayer) {
        synchronized (mLock) {
            mDelayer = delayer;
        }
    }


    /*哈哈,查询方法就是这个入口,形参是一个CharSequence 类型的数据*/
    public final void filter(CharSequence constraint) {
        filter(constraint, null);
    }


    
    public final void filter(CharSequence constraint, FilterListener listener) {
        synchronized (mLock) {
            /*一个线程下的RequestHandler*/
            if (mThreadHandler == null) {
                HandlerThread thread = new HandlerThread(
                        THREAD_NAME, android.os.Process.THREAD_PRIORITY_BACKGROUND);
                thread.start();
               //注意这里产生了一个RequestHandler对象,消息发出后调用performFiltering(CharSequence constraint);
                mThreadHandler = new RequestHandler(thread.getLooper()); 
            }

            final long delay = (mDelayer == null) ? 0 : mDelayer.getPostingDelay(constraint);
            
           //这个就是要发送的消息了,发出去的消息将会在RequestHandler中的handleMessage()得到处理
            Message message = mThreadHandler.obtainMessage(FILTER_TOKEN); 
            
    
            RequestArguments args = new RequestArguments();
            // make sure we use an immutable copy of the constraint, so that
            // it doesn't change while the filter operation is in progress
            args.constraint = constraint != null ? constraint.toString() : null;  //把输入的查询信息转化成字符串
            args.listener = listener;
            message.obj = args;
    
            /*先把消息FILTER_TOKEN和FINISH_TOKEN移除,然后再把上面得到的FILTER_TOKEN消息发送出去,发出去的消息将会在RequestHandler中  的handleMessage()得到处理*/
            mThreadHandler.removeMessages(FILTER_TOKEN);
            mThreadHandler.removeMessages(FINISH_TOKEN);
            mThreadHandler.sendMessageDelayed(message, delay);
        }
    }

    /*看,这里是一个抽象的方法,刚才已经在ArrayAdapter中得到具体的实现了,是在new RequestHandler中的 public void handleMessage(Message   msg)被调用的*/
    protected abstract FilterResults performFiltering(CharSequence constraint);  

    //看,这也是一个抽象的方法,也已经在ArrayAdapter中得到具体的实现了,这个是在用户查询时会被调用,也在ArrayAdapter中实现了
    protected abstract void publishResults(CharSequence constraint,
            FilterResults results);

    
    public CharSequence convertResultToString(Object resultValue) {
        return resultValue == null ? "" : resultValue.toString();
    }

    
    protected static class FilterResults {
        public FilterResults() {
            // nothing to see here
        }
     
        public Object values;

        public int count;
    }

    
    public static interface FilterListener {
        public void onFilterComplete(int count);
    }

    
    private class RequestHandler extends Handler {
        public RequestHandler(Looper looper) {
            super(looper);
        }
        
       
 /*处理从public final void filter(CharSequence constraint)中发出来的消息*/
        public void handleMessage(Message msg) {
            int what = msg.what;
            Message message;
            switch (what) {
                case FILTER_TOKEN:
                    RequestArguments args = (RequestArguments) msg.obj;
                    try {
                        args.results = performFiltering(args.constraint);//这里就是上面的抽象方法了,在
ArrayAdapter中得到实现了
                    } catch (Exception e) {
                        args.results = new FilterResults();
                        Log.w(LOG_TAG, "An exception occured during performFiltering()!", e);
                    } finally {
                        message = mResultHandler.obtainMessage(what);
                        message.obj = args;
                        message.sendToTarget();
                    }

                    synchronized (mLock) {
                        if (mThreadHandler != null) {
                            Message finishMessage = mThreadHandler.obtainMessage(FINISH_TOKEN);
                            mThreadHandler.sendMessageDelayed(finishMessage, 3000); /*处理完查询后,mThreadHandler停掉自己,延时3秒发送*/
                        }
                    }
                    break;
                case FINISH_TOKEN:
                    synchronized (mLock) {
                        if (mThreadHandler != null) {
                            mThreadHandler.getLooper().quit();
                            mThreadHandler = null;
                        }
                    }
                    break;
            }
        }
    }


    
    private class ResultsHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            RequestArguments args = (RequestArguments) msg.obj;

            publishResults(args.constraint, args.results);
            if (args.listener != null) {
                int count = args.results != null ? args.results.count : -1;
                args.listener.onFilterComplete(count);
            }
        }
    }

    
    private static class RequestArguments {
        CharSequence constraint;

        FilterListener listener;
        FilterResults results;
    }

    public interface Delayer {
        long getPostingDelay(CharSequence constraint);
    }
}

综上所述,ArrayAdapter就是一个具有对适配器进行查询、删除、排序、插入的BaseAdapter,这些功能在于ArrayAdapter的类似数组的接口,还有一个很重要的可以用来查询过滤数据的实例mFliter,从getFilter()方法中得到一个Filter,比如AFilter = getFilter();AFliter.filter("abc");就可以查询有关abc的有关内容了,最后条用notifyDataSetChanged()来更新得到的数据。

原创粉丝点击