深刻分析ArrayAdapter

昨天看了一下ArrayAdapter的源代碼,發現它還真把BaseAdapter封裝的不錯。故今天從源碼的角度來看看ArrayAdapter。java

搞android都知道ArrayAdapter的基本用法,就是在初始化ArrayAdapter的時候,無論你調用那一個構造類,都會調用這個方法:android

 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;
    }

而後當你的ListView調用setAdapter的時候,ArrayAdapter就回調用這個方法:
網絡

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


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) {
            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;
    }

但咱們發現沒有,咱們ArrayAdapter是一個實體類,而咱們在實際工做中大部分用到的的BaseAdapter是一個抽象類,因此咱們必須須要重寫BaseAdapter中的四個抽象方法,但在ArrayAdapter中咱們就不須要了,看代碼:ide

    /**
     * {@inheritDoc}
     */
    public int getCount() {
        return mObjects.size();
    }

    /**
     * {@inheritDoc}
     */
    public T getItem(int position) {
        return mObjects.get(position);
    }


    /**
     * {@inheritDoc}
     */
    public long getItemId(int position) {
        return position;
    }
    /**
     * {@inheritDoc}
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        return createViewFromResource(position, convertView, parent, mResource);
    }

在ArrayAdapter中已經把這四個抽象方法給實現了,那從此咱們在設置setAdapter的時候,讓咱們的自定義Adapter直接繼承ArrayAdapter,而後重寫getView豈不是更好,而後咱們就和那三個基本沒有意義的三個抽象方法說拜拜了。佈局

而後咱們在看ArrayAdapter,發現它還實現了Filterable這個接口,而後重寫了:ui

 /**
     * {@inheritDoc}
     */
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ArrayFilter();
        }
        return mFilter;
    }

這個方法,有的人會問這個方法有什麼用啊,那就得說說Filter這個抽象類了,在ArrayAdapter中有一個成員內部類:spa

    /**
     * <p>An array filter constrains the content of the array adapter with
     * a prefix. Each item that does not start with the supplied prefix
     * is removed from the list.</p>
     */
    private class ArrayFilter extends 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;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            //noinspection unchecked
            mObjects = (List<T>) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }

這個ArrayFilter內部類實現了這個Filter抽象類的兩個抽象方法,一個是performFiltering,一個是publishResult,在performFiltering中實現了過濾條件,那麼試想一下,若是公司有搜索聯繫人這麼一個相似須要,佈局大約是上邊一個EditText,下邊是一個ListView,數據都在本地,不用網絡獲取了,那咱們怎麼經過輸入的文字快速的過濾後展現到ListView中呢,那麼這裏就有了思路。code

咱們看一下ArrayFilter中的:orm

final String valueText = value.toString().toLowerCase();

意思就是把bean中的toString方法做爲比較條件,咱們是否是在bean中重寫一些toString方法,而後在EditText的監聽中調用arrayAdapter.getFilter().filter(CharSequence constraint)不就OK了,也有人說了,你本身不會寫個過濾條件啊,可是有現成的爲何不用呢。繼承

此外ArrayAdapter裏還有一些使用的方法,咱們能夠慢慢體會,扔掉那個沒怎麼封裝的BaseAdapter吧。

相關文章
相關標籤/搜索