public abstract class AbsArrayAdapter<T> extends BaseAdapter implements Filterable
A custom abstract BaseAdapter
that is backed by an ArrayList
of arbitrary
objects. By default this class delegates view generation and defining the filtering logic to
subclasses.
Designed to be a more flexible and customizable solution then Android's ArrayAdapter class. It
provides extra features such as: supporting additional ArrayList
methods, resolves
outstanding filtering bugs, makes smarter use of notifyDataSetChanged()
, and
conveniently passes along a layout inflater for view creation.
Because of the background filtering process, all methods which mutates the underlying data are
internally synchronized. This ensures a thread safe environment for internal write operations. If
filtering is not required, it's strongly recommended to use the NFArrayAdapter
instead.
IGNORE_ITEM_VIEW_TYPE, NO_SELECTION
Constructor and Description |
---|
AbsArrayAdapter(Context activity)
Constructor
|
AbsArrayAdapter(Context activity,
Collection<T> items)
Constructor
|
AbsArrayAdapter(Context activity,
T[] items)
Constructor
|
Modifier and Type | Method and Description |
---|---|
void |
add(T items)
Adds the specified items at the end of the adapter.
|
void |
addAll(Collection<? extends T> items)
Adds the specified Collection at the end of the adapter.
|
void |
addAll(T... items)
Adds the specified items at the end of the adapter.
|
void |
clear()
Remove all elements from the adapter.
|
boolean |
contains(T item)
Tests whether this adapter contains the specified item.
|
boolean |
containsAll(Collection<?> items)
Tests whether this adapter contains all items contained in the specified collection.
|
Context |
getContext() |
int |
getCount() |
View |
getDropDownView(int position,
View convertView,
ViewGroup parent) |
View |
getDropDownView(LayoutInflater inflater,
int position,
View convertView,
ViewGroup parent)
Get a
View that displays in the drop down popup the data at the
specified position in the data set. |
Filter |
getFilter() |
ArrayList<T> |
getFilteredList() |
T |
getItem(int position) |
long |
getItemId(int position) |
ArrayList<T> |
getList() |
int |
getPosition(T item)
Returns the position of the specified item in the array.
|
View |
getView(int position,
View convertView,
ViewGroup parent) |
abstract View |
getView(LayoutInflater inflater,
int position,
View convertView,
ViewGroup parent)
Get a View that displays the data at the specified position in the data set.
|
void |
notifyDataSetChanged() |
void |
remove(T item)
Removes the first occurrence of the specified item from the adapter.
|
void |
removeAll(Collection<?> items)
Removes all occurrences in the adapter of each item in the specified collection.
|
void |
retainAll(Collection<?> items)
Removes all items from this adapter that are not contained in the specified collection.
|
void |
setList(Collection<? extends T> items)
Resets the adapter to store a new list of items.
|
void |
setNotifyOnChange(boolean notifyOnChange)
Control whether methods that change the list (
add(T) , retainAll(java.util.Collection<?>) , remove(T) , clear() ) automatically call notifyDataSetChanged() . |
void |
sort()
Sorts the content of this adapter using the natural order of the stored items themselves.
|
void |
sort(Comparator<? super T> comparator)
Sorts the content of this adapter using the specified comparator.
|
void |
update(int position,
T item)
Updates the item at the specified position in the adapter with the specified item.
|
areAllItemsEnabled, getItemViewType, getViewTypeCount, hasStableIds, isEmpty, isEnabled, notifyDataSetInvalidated, registerDataSetObserver, unregisterDataSetObserver
public AbsArrayAdapter(Context activity)
activity
- Context used for inflating viewspublic AbsArrayAdapter(Context activity, T[] items)
activity
- Context used for inflating viewsitems
- The items to represent within the adapter.public AbsArrayAdapter(Context activity, Collection<T> items)
activity
- Context used for inflating viewsitems
- The items to represent within the adapter.public void add(T items)
items
- The items to add at the end of the adapter.public void addAll(Collection<? extends T> items)
items
- The Collection to add at the end of the adapter.@SafeVarargs public final void addAll(T... items)
items
- The items to add at the end of the adapter.public void clear()
public boolean contains(T item)
item
- The item to search fortrue
if the item is an element of this adapter. false
otherwisepublic boolean containsAll(Collection<?> items)
items
- The collection of itemstrue
if all items in the specified collection are elements of this adapter,
false
otherwisepublic Context getContext()
public View getDropDownView(LayoutInflater inflater, int position, View convertView, ViewGroup parent)
Get a View
that displays in the drop down popup the data at the
specified position in the data set.
inflater
- the LayoutInflater object that can be used to inflate each view.position
- index of the item whose view we want.convertView
- the old view to reuse, if possible. Note: You should check that this view
is non-null and of an appropriate type before using. If it is not possible
to convert this view to display the correct data, this method can create a
new view.parent
- the parent that this view will eventually be attached toView
corresponding to the data at the specified position.public final View getDropDownView(int position, View convertView, ViewGroup parent)
getDropDownView
in interface SpinnerAdapter
getDropDownView
in class BaseAdapter
public Filter getFilter()
getFilter
in interface Filterable
public ArrayList<T> getFilteredList()
public ArrayList<T> getList()
public void setList(Collection<? extends T> items)
clear()
,
then addAll(java.util.Collection)
without having to worry about an extra notifyDataSetChanged()
invoked in between. Will repeat the last filtering request if
invoked while filtered results are being displayed.items
- New list of items to store within the adapter.public int getPosition(T item)
item
- The item to retrieve the position of.public abstract View getView(LayoutInflater inflater, int position, View convertView, ViewGroup parent)
LayoutInflater.inflate(int, android.view.ViewGroup, boolean)
to specify
a root view and to prevent attachment to the root.inflater
- The LayoutInflater object that can be used to inflate each view.position
- The position of the item within the adapter's data set of the item whose
view we want.convertView
- The old view to reuse, if possible. Note: You should check that this view
is non-null and of an appropriate type before using. If it is not possible
to convert this view to display the correct data, this method can create a
new view. Heterogeneous lists can specify their number of view types, so
that this View is always of the right type (see BaseAdapter.getViewTypeCount()
and BaseAdapter.getItemViewType(int)
).parent
- The parent that this view will eventually be attached topublic void notifyDataSetChanged()
notifyDataSetChanged
in class BaseAdapter
public void remove(T item)
item
- The item to remove.public void removeAll(Collection<?> items)
items
- The collection of items to removepublic void retainAll(Collection<?> items)
items
- The collection of items to retainpublic void setNotifyOnChange(boolean notifyOnChange)
Control whether methods that change the list (add(T)
, retainAll(java.util.Collection<?>)
, remove(T)
, clear()
) automatically call 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.
notifyOnChange
- if true, modifications to the list will automatically call notifyDataSetChanged()
public void sort()
Comparable
and is equivalent of
passing null to sort(java.util.Comparator)
.ClassCastException
- If the comparator is null and the stored items do not
implement Comparable
or if compareTo
throws for any pair of items.public void sort(Comparator<? super T> comparator)
comparator
- Used to sort the items contained in this adapter. Null to use an item's
Comparable
interface.ClassCastException
- If the comparator is null and the stored items do not
implement Comparable
or if compareTo
throws for any pair of items.public void update(int position, T item)
position
- The location at which to put the specified itemitem
- The new item to replace with the old