public abstract class NFArrayAdapter<T> extends BaseAdapter
A non-filterable custom abstract BaseAdapter
that is backed by an ArrayList
of
arbitrary objects. By default this class delegates view generation to subclasses.
Designed to be a more flexible and customizable solution then Android's ArrayAdapter class but
without the filtering mechanism. As a result, there is no need for synchronized
blocks
which may help those worried about performance. It provides extra features such as: supporting
additional ArrayList
methods, makes smarter use of notifyDataSetChanged()
, and
conveniently passes along a layout inflater for view creation.
If filtering is required, it's strongly recommended to use the AbsArrayAdapter
instead.
IGNORE_ITEM_VIEW_TYPE, NO_SELECTION
Constructor and Description |
---|
NFArrayAdapter(Context activity)
Constructor
|
NFArrayAdapter(Context activity,
Collection<T> items)
Constructor
|
NFArrayAdapter(Context activity,
T[] items)
Constructor
|
Modifier and Type | Method and Description |
---|---|
void |
add(T item)
Adds the specified item 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. |
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 |
insert(int index,
T item)
Inserts the specified item at the specified index in the array.
|
void |
insertAll(int index,
Collection<? extends T> items)
Inserts the item in the specified collection at the specified location in this adapter.
|
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) , insert(int, T) , 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 NFArrayAdapter(Context activity)
activity
- Context used for inflating viewspublic NFArrayAdapter(Context activity, Collection<T> items)
activity
- Context used for inflating viewsitems
- The items to represent within the adapter.public void add(T item)
item
- The item 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 int getCount()
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 T getItem(int position)
public long getItemId(int position)
public void setList(Collection<? extends T> items)
clear()
,
then addAll(java.util.Collection)
without having to worry about an extra notifyDataSetChanged()
invoked in between.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 insert(int index, T item)
index
- The index at which the item must be inserted.item
- The item to insert into the adapter.public void insertAll(int index, Collection<? extends T> items)
index
- The index at which the items must be inserted.items
- The collection of items to be inserted.public 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)
, insert(int, T)
, 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