public abstract class RolodexArrayAdapter<G,C> extends PatchedExpandableListAdapter implements Filterable
For use with an ExpandableListView
, The rolodex adapters are specifically designed to
tackle the problem of organizing existing data on the fly without the need to pre-compute the
groupings nor actually store the grouping data itself. Instead of having to organize your data
ahead of time, you can simply pass in a list of arbitrary data and provide one simple method
which determines the groupings it belongs to. Though not required, ideally this relationship
would be derived from the data itself. For example, populating the adapter with a list of Person
objects could derive it's groupings based on the last name. Just like an old-school rolodex.
The RolodexArrayAdapter uses a Map
to organize the data under each group. The children
within each grouping are backed by an ArrayList
. The data can be easily modified and
filtered in various ways and allows numerous display and sorting options. Additionally full
support for ChoiceMode
is available. By default this class delegates view
generation and defining the filtering logic to subclasses.
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 NFRolodexArrayAdapter
instead.
PatchedExpandableListAdapter.ChoiceMode, PatchedExpandableListAdapter.ChoiceModeListener
Constructor and Description |
---|
RolodexArrayAdapter(Context activity)
Constructor
|
RolodexArrayAdapter(Context activity,
C[] items)
Constructor
|
RolodexArrayAdapter(Context activity,
Collection<C> items)
Constructor
|
Modifier and Type | Method and Description |
---|---|
void |
add(C childItem)
Adds the specified items at the end of the adapter.
|
void |
addAll(C... childItems)
Adds the specified child items at the end of the adapter.
|
void |
addAll(Collection<? extends C> childItems)
Adds the specified Collection at the end of the adapter.
|
boolean |
areGroupsSorted() |
void |
clear()
Remove all elements from the adapter.
|
boolean |
contains(C childItem)
Tests whether this adapter contains the specified child item.
|
abstract G |
createGroupFor(C childItem)
Creates a new group object which represents the parent of the given child item.
|
C |
getChild(int groupPosition,
int childPosition) |
long |
getChildId(int groupPosition,
int childPosition) |
int |
getChildrenCount(int groupPosition) |
Filter |
getFilter() |
ArrayList<C> |
getFilteredList() |
G |
getGroup(int groupPosition) |
ArrayList<C> |
getGroupChildren(int groupPosition)
Gets all the children associated with the given group.
|
int |
getGroupCount() |
G |
getGroupFor(C childItem)
Retrieves a group object for the given child.
|
G |
getGroupFromCacheFor(C childItem)
Override to provide a caching mechanism for retrieving a group item.
|
long |
getGroupId(int groupPosition) |
ArrayList<C> |
getList() |
void |
notifyDataSetChanged() |
void |
remove(C childItem)
Removes the first occurrence of the specified child item from the adapter.
|
void |
removeAll(Collection<? extends C> childItems)
Convenience method which removes all occurrences in the adapter of each item in the specified
collection.
|
void |
retainAll(Collection<?> childItems)
Removes all children items from this adapter that are not contained in the specified
collection.
|
void |
setList(Collection<? extends C> childItems)
Resets the adapter to store a new list of children items.
|
void |
setNotifyOnChange(boolean notifyOnChange)
Controls whether methods that change the list (
add(C) , remove(C) , clear() ) automatically call notifyDataSetChanged() . |
void |
sortAllChildren()
Sorts the children of each grouping using the natural order of the items themselves.
|
void |
sortAllChildren(Comparator<? super C> comparator)
Sorts the children of each grouping using the specified comparator.
|
void |
sortGroup(int groupPosition)
Sorts the children of the specified group using the natural order of the children themselves.
|
void |
sortGroup(int groupPosition,
Comparator<? super C> comparator)
Sorts the children of the specified group using the specified comparator.
|
void |
update(int groupPosition,
int childPosition,
C childItem)
Updates the item at the specified position in the adapter with the specified item.
|
clearChoices, collapseAll, expandAll, expandAll, getCheckedChildCount, getCheckedChildIds, getCheckedChildPositions, getCheckedGroupCount, getCheckedGroupIds, getCheckedGroupPositions, getChildView, getChildView, getChoiceMode, getContext, getGroupView, getGroupView, hasAutoExpandingGroups, hasStableIds, isChildChecked, isChildSelectable, isGroupChecked, isGroupSelectable, onRestoreInstanceState, onSaveInstanceState, setChildChecked, setChoiceMode, setGroupChecked, setMultiChoiceModeListener, setOnChildClickListener, setOnGroupClickListener, startActionMode
areAllItemsEnabled, getChildType, getChildTypeCount, getCombinedChildId, getCombinedGroupId, getGroupType, getGroupTypeCount, isEmpty, notifyDataSetInvalidated, onGroupCollapsed, onGroupExpanded, registerDataSetObserver, unregisterDataSetObserver
public RolodexArrayAdapter(Context activity)
activity
- Context used for inflating viewspublic RolodexArrayAdapter(Context activity, C[] items)
activity
- Context used for inflating viewsitems
- The items to represent within the adapter.public RolodexArrayAdapter(Context activity, Collection<C> items)
activity
- Context used for inflating viewsitems
- The items to represent within the adapter.public void add(C childItem)
childItem
- The child item to add at the end of the adapter.public void addAll(Collection<? extends C> childItems)
childItems
- The Collection of children items to add at the end of the adapter.@SafeVarargs public final void addAll(C... childItems)
childItems
- The child items to add at the end of the adapter.public boolean areGroupsSorted()
public void clear()
public boolean contains(C childItem)
childItem
- The child item to search fortrue
if the child item is an element of this adapter. false
otherwisepublic abstract G createGroupFor(C childItem)
Creates a new group object which represents the parent of the given child item. This is
used to determine what group the child item will fall under. Do not attempt to return a
cached group object here. See getGroupFromCacheFor(Object)
for that behavior.
It's recommended that the group object returned is immutable, or whose hashcode is based on an immutable field(s). A mutable object is fine so long as it's not modified during the lifespan of this adapter.
childItem
- The child item for which a group instance will be created for.public C getChild(int groupPosition, int childPosition)
getChild
in interface ExpandableListAdapter
public long getChildId(int groupPosition, int childPosition)
getChildId
in interface ExpandableListAdapter
public int getChildrenCount(int groupPosition)
getChildrenCount
in interface ExpandableListAdapter
public Filter getFilter()
getFilter
in interface Filterable
public ArrayList<C> getFilteredList()
public G getGroup(int groupPosition)
getGroup
in interface ExpandableListAdapter
public ArrayList<C> getGroupChildren(int groupPosition)
groupPosition
- The position of the group.public int getGroupCount()
getGroupCount
in interface ExpandableListAdapter
public final G getGroupFor(C childItem)
getGroupFromCacheFor(Object)
if you wish to provide your own cache implementation.childItem
- Child item to look forpublic G getGroupFromCacheFor(C childItem)
Override to provide a caching mechanism for retrieving a group item. Caching can help
reduce the number of createGroupFor(Object)
invocations. By default, no caching is
provided by the adapter. This method normally returns null.
Pulling from cache is primarily used when mutating the adapter. It is never used nor needed by any of the getters.
Additionally, the group object returned should be immutable, or whose hashcode is based on an immutable field(s). A mutable object is fine so long as it's not modified during the lifespan of this adapter.
childItem
- The child item for which a group object will be returned for.public long getGroupId(int groupPosition)
getGroupId
in interface ExpandableListAdapter
public ArrayList<C> getList()
public void setList(Collection<? extends C> childItems)
clear()
, then addAll(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.childItems
- New list of children items to store within the adapter.public void notifyDataSetChanged()
notifyDataSetChanged
in class BaseExpandableListAdapter
public void remove(C childItem)
childItem
- The child item to remove.public void removeAll(Collection<? extends C> childItems)
childItems
- The collection of child items to removepublic void retainAll(Collection<?> childItems)
childItems
- The collection of children items to retainpublic void setNotifyOnChange(boolean notifyOnChange)
Controls whether methods that change the list (add(C)
, remove(C)
, 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 sortAllChildren()
Comparable
and is equivalent of passing null
to sortAllChildren(Comparator)
. This will not sort groups themselves.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 sortAllChildren(Comparator<? super C> comparator)
comparator
- Used to sort the child 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 sortGroup(int groupPosition)
Comparable
and is equivalent of
passing null to sortGroup(int, Comparator)
. This will not sort groups themselves.ClassCastException
- If the comparator is null and the stored children do not
implement Comparable
, or if compareTo
throws for any pair of items.public void sortGroup(int groupPosition, Comparator<? super C> comparator)
comparator
- Used to sort the child items contained within a group. Null to use an
item's Comparable
interface.ClassCastException
- If the comparator is null and the stored children do not
implement Comparable
, or if compareTo
throws for any pair of items.public void update(int groupPosition, int childPosition, C childItem)
groupPosition
- The group location at which to put the specified childchildPosition
- The child location at which to put the specified itemchildItem
- The new item to replace with the old