public abstract class NFSparseAdapter<T> extends BaseAdapter
A non-filterable custom abstract BaseAdapter
that is backed by a SparseArray
of arbitrary objects. By default this class delegates view generation to subclasses.
Designed to be a flexible and customizable solution for using SparseArray with an adapter but
without the filtering mechanism. As a result, there is no need for synchronized
blocks
which may help those worried about performance. It exposes most of the SparseArray methods, and
conveniently passes along a layout inflater for view creation. An adapter's row ID maps to the
SparseArray's key and vice versa. Any method requiring a key will have "withId" in the
name.
If filtering is required, it's strongly recommended to use the SparseAdapter
instead.
IGNORE_ITEM_VIEW_TYPE, NO_SELECTION
Constructor and Description |
---|
NFSparseAdapter(Context activity)
Constructor
|
NFSparseAdapter(Context activity,
SparseArray<T> items)
Constructor
|
Modifier and Type | Method and Description |
---|---|
void |
appendAll(SparseArray<T> items)
Appends the specified SparseArray at the end of the adapter, optimizing for the case where
all the keys are greater then all existing keys in the adapter.
|
void |
appendWithId(int keyId,
T item)
Appends the specified key and item pair to the end of the adapter, optimizing for the case
where the key is greater then all existing keys in the adapter.
|
void |
clear()
Removes all items from the adapter.
|
boolean |
containsId(int keyId)
Determines if the specified keyId exists within the adapter.
|
boolean |
containsItem(T item)
Determines if the specified item exists within the adapter.
|
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) |
T |
getItemWithId(int keyId) |
int |
getPosition(int keyId) |
int |
getPosition(T item)
Returns the position of the specified item in the sparse array.
|
SparseArray<T> |
getSparseArray() |
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 |
put(int position,
T item)
Given a position in the range of 0...
getCount() - 1, sets a new value for the
key-value stored at that position. |
void |
putAll(SparseArray<T> items)
Adds the specified SparseArray to the adapter, replacing any existing mappings from the
specified keys if there were any.
|
void |
putWithId(int keyId,
T item)
Adds the specified key and item to the adapter, replacing the previous mapping from the
specified key if there was one.
|
void |
remove(int position)
Removes the mapping at the specified position in the adapter.
|
void |
removeAll(SparseArray<T> items)
Removes all items from the adapter that are found within the specified SparseArray.
|
void |
removeWithId(int keyId)
Removes the mapping with the specified keyId from the adapter.
|
void |
setNotifyOnChange(boolean notifyOnChange)
Controls whether methods that change the list (
appendWithId(int, T) , putWithId(int, T) ,
remove(int) , clear() ) automatically call notifyDataSetChanged() . |
void |
setSparseArray(SparseArray<T> items)
Resets the adapter to store a new SparseArray of items.
|
areAllItemsEnabled, getItemViewType, getViewTypeCount, hasStableIds, isEmpty, isEnabled, notifyDataSetInvalidated, registerDataSetObserver, unregisterDataSetObserver
public NFSparseAdapter(Context activity)
activity
- Context used for inflating viewspublic NFSparseAdapter(Context activity, SparseArray<T> items)
activity
- Context used for inflating viewsitems
- The items to represent within the adapter.public void appendAll(SparseArray<T> items)
items
- The SparseArray items to add at the end of the adapter.public void appendWithId(int keyId, T item)
keyId
- The keyId to append withitem
- The item to append withpublic void clear()
public boolean containsId(int keyId)
keyId
- The keyId to search fortrue
if the keyId is found within the adapter. false
otherwise.public boolean containsItem(T item)
Determines if the specified item exists within the adapter. Be aware that this is a linear search, unlike look-ups by key, and that multiple keys can map to the same value and this will find only one of them.
Note also that unlike most collections this method compares values using == rather than equals...a result of how SparseArrays are implemented.
item
- The item to search fortrue
if the item is an element 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 T getItemWithId(int keyId)
public int getPosition(T item)
Returns the position of the specified item in the sparse array. Be aware that this is a linear search, unlike look-ups by key,and that multiple keys can map to the same value and this will find only one of them.
Note also that unlike most collections, this method compares values using == rather than equals.
item
- The item to retrieve the position of.public int getPosition(int keyId)
keyId
- The keyId to search forpublic SparseArray<T> getSparseArray()
public void setSparseArray(SparseArray<T> items)
clear()
, followed by putAll(android.util.SparseArray<T>)
; this method is more efficient. It performs a straight
clone of the items instead of having to perform a binary search before putting each item
within the adapter.items
- New SparseArray of items to store within the adapter.public abstract View getView(LayoutInflater inflater, int position, View convertView, ViewGroup parent)
to specifiy 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 wantconvertView
- 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 topublic void notifyDataSetChanged()
notifyDataSetChanged
in class BaseAdapter
public void put(int position, T item)
getCount()
- 1, sets a new value for the
key-value stored at that position.position
- The position of the item to updateitem
- The item to update withpublic void putAll(SparseArray<T> items)
items
- The SparseArray items to add to the adapter.public void putWithId(int keyId, T item)
public void remove(int position)
position
- The position of the item to removepublic void removeAll(SparseArray<T> items)
items
- The SparseArray items to remove from the adapter.public void removeWithId(int keyId)
keyId
- The keyId to remove.public void setNotifyOnChange(boolean notifyOnChange)
Controls whether methods that change the list (appendWithId(int, T)
, putWithId(int, T)
,
remove(int)
, 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()