/* * Copyright (C) 2006 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * This ArrayList class is copied from android.widget.ArrayList and * adjusted in three ways: 1) loading from resources is no longer supported, * 2) a public method is added to allow replacing of the while list, and * 3) it is made abstract, requiring to override getView (no more setting * of the resource id to show) */ package com.ratebeer.android.gui.components.helpers; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Filter; import android.widget.Filterable; /** * A ListAdapter that manages a ListView backed by an array of arbitrary * objects. getView needs to be overridden to determine the view to show * for an object in the list. */ public abstract class ArrayAdapter<T> extends BaseAdapter implements Filterable { /** * Contains the list of objects that represent the data of this ArrayAdapter. * The content of this list is referred to as "the array" in the documentation. */ private List<T> mObjects; /** * Lock used to modify the content of {@link #mObjects}. Any write operation * performed on the array should be synchronized on this lock. This lock is also * used by the filter (see {@link #getFilter()} to make a synchronized copy of * the original array of data. */ private final Object mLock = new Object(); /** * Indicates whether or not {@link #notifyDataSetChanged()} must be called whenever * {@link #mObjects} is modified. */ private boolean mNotifyOnChange = true; private Context mContext; private ArrayList<T> mOriginalValues; private ArrayFilter mFilter; /** * Constructor * * @param context The current context. */ public ArrayAdapter(Context context) { init(context, new ArrayList<T>()); } /** * Constructor * * @param context The current context. * @param objects The objects to represent in the ListView. */ public ArrayAdapter(Context context, T[] objects) { init(context, Arrays.asList(objects)); } /** * Constructor * * @param context The current context. * @param objects The objects to represent in the ListView. */ public ArrayAdapter(Context context, List<T> objects) { init(context, objects); } /** * Adds the specified object at the end of the array. * * @param object The object to add at the end of the array. */ public void add(T object) { if (mOriginalValues != null) { synchronized (mLock) { mOriginalValues.add(object); if (mNotifyOnChange) notifyDataSetChanged(); } } else { mObjects.add(object); if (mNotifyOnChange) notifyDataSetChanged(); } } /** * Adds a collection of objects at the end of the array. * * @param object The collection to add at the end of the array. */ public void addAll(Collection<? extends T> object) { if (mOriginalValues != null) { synchronized (mLock) { mOriginalValues.addAll(object); if (mNotifyOnChange) notifyDataSetChanged(); } } else { mObjects.addAll(object); if (mNotifyOnChange) notifyDataSetChanged(); } } /** * Inserts the specified object at the specified index in the array. * * @param object The object to insert into the array. * @param index The index at which the object must be inserted. */ public void insert(T object, int index) { if (mOriginalValues != null) { synchronized (mLock) { mOriginalValues.add(index, object); if (mNotifyOnChange) notifyDataSetChanged(); } } else { mObjects.add(index, object); if (mNotifyOnChange) notifyDataSetChanged(); } } /** * Removes the specified object from the array. * * @param object The object to remove. */ public void remove(T object) { if (mOriginalValues != null) { synchronized (mLock) { mOriginalValues.remove(object); } } else { mObjects.remove(object); } if (mNotifyOnChange) notifyDataSetChanged(); } /** * Remove all elements from the list. */ public void clear() { if (mOriginalValues != null) { synchronized (mLock) { mOriginalValues.clear(); } } else { mObjects.clear(); } if (mNotifyOnChange) notifyDataSetChanged(); } /** * Sorts the content of this adapter using the specified comparator. * * @param comparator The comparator used to sort the objects contained * in this adapter. */ public void sort(Comparator<? super T> comparator) { Collections.sort(mObjects, comparator); if (mNotifyOnChange) notifyDataSetChanged(); } /** * Replaces the underlying list some other list and notifies a listening view. * * @param objects The new list of data objects, */ public void replace(List<T> objects) { init(getContext(), objects); if (mNotifyOnChange) notifyDataSetChanged(); } /** * {@inheritDoc} */ @Override public void notifyDataSetChanged() { super.notifyDataSetChanged(); mNotifyOnChange = true; } /** * Control whether methods that change the list ({@link #add}, * {@link #insert}, {@link #remove}, {@link #clear}) automatically call * {@link #notifyDataSetChanged}. If set to false, caller must * manually call notifyDataSetChanged() to have the changes * reflected in the attached view. * * The default is true, and calling notifyDataSetChanged() * resets the flag to true. * * @param notifyOnChange if true, modifications to the list will * automatically call {@link * #notifyDataSetChanged} */ public void setNotifyOnChange(boolean notifyOnChange) { mNotifyOnChange = notifyOnChange; } private void init(Context context, List<T> objects) { mContext = context; mObjects = objects; } /** * Returns the context associated with this array adapter. The context is used * to create views from the resource passed to the constructor. * * @return The Context associated with this adapter. */ public Context getContext() { return mContext; } /** * {@inheritDoc} */ public int getCount() { return mObjects.size(); } /** * {@inheritDoc} */ public T getItem(int position) { return mObjects.get(position); } /** * Returns all objects in this adapter. * * @return The contained list of objects. */ public List<T> getAllItems() { return mObjects; } /** * Returns the position of the specified item in the array. * * @param item The item to retrieve the position of. * * @return The position of the specified item. */ public int getPosition(T item) { return mObjects.indexOf(item); } /** * {@inheritDoc} */ public long getItemId(int position) { return position; } /** * {@inheritDoc} */ public abstract View getView(int position, View convertView, ViewGroup parent); /** * {@inheritDoc} */ public Filter getFilter() { if (mFilter == null) { mFilter = new ArrayFilter(); } return mFilter; } /** * <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) { synchronized (mLock) { ArrayList<T> list = new ArrayList<T>(mOriginalValues); results.values = list; results.count = list.size(); } } else { String prefixString = prefix.toString().toLowerCase(); final ArrayList<T> values = mOriginalValues; final int count = values.size(); final ArrayList<T> newValues = new ArrayList<T>(count); 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; 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; } @SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint, FilterResults results) { //noinspection unchecked mObjects = (List<T>) results.values; if (results.count > 0) { notifyDataSetChanged(); } else { notifyDataSetInvalidated(); } } } }