AlphabetIndexer
open class AlphabetIndexer : DataSetObserver, SectionIndexer
kotlin.Any | ||
↳ | android.database.DataSetObserver | |
↳ | android.widget.AlphabetIndexer |
A helper class for adapters that implement the SectionIndexer interface. If the items in the adapter are sorted by simple alphabet-based sorting, then this class provides a way to do fast indexing of large lists using binary search. It caches the indices that have been determined through the binary search and also invalidates the cache if changes occur in the cursor.
Your adapter is responsible for updating the cursor by callingsetCursor
if the cursor changes. getPositionForSection
method does the binary search for the starting index of a given section (alphabet).
Summary
Public constructors | |
---|---|
AlphabetIndexer(cursor: Cursor!, sortedColumnIndex: Int, alphabet: CharSequence!) Constructs the indexer. |
Public methods | |
---|---|
open Int |
getPositionForSection(sectionIndex: Int) Performs a binary search or cache lookup to find the first row that matches a given section's starting letter. |
open Int |
getSectionForPosition(position: Int) Returns the section index for a given position in the list by querying the item and comparing it with all items in the section array. |
open Array<Any!>! |
Returns the section array constructed from the alphabet provided in the constructor. |
open Unit | |
open Unit | |
open Unit |
Sets a new cursor as the data set and resets the cache of indices. |
Protected methods | |
---|---|
open Int |
Default implementation compares the first character of word with letter. |
Properties | |
---|---|
CharSequence! |
The string of characters that make up the indexing sections. |
Int |
The index of the cursor column that this list is sorted on. |
Cursor! |
Cursor that is used by the adapter of the list view. |
Public constructors
AlphabetIndexer
AlphabetIndexer(
cursor: Cursor!,
sortedColumnIndex: Int,
alphabet: CharSequence!)
Constructs the indexer.
Parameters | |
---|---|
cursor |
Cursor!: the cursor containing the data set |
sortedColumnIndex |
Int: the column number in the cursor that is sorted alphabetically |
alphabet |
CharSequence!: string containing the alphabet, with space as the first character. For example, use the string " ABCDEFGHIJKLMNOPQRSTUVWXYZ" for English indexing. The characters must be uppercase and be sorted in ascii/unicode order. Basically characters in the alphabet will show up as preview letters. |
Public methods
getPositionForSection
open fun getPositionForSection(sectionIndex: Int): Int
Performs a binary search or cache lookup to find the first row that matches a given section's starting letter.
Parameters | |
---|---|
sectionIndex |
Int: the section to search for |
Return | |
---|---|
Int |
the row index of the first occurrence, or the nearest next letter. For instance, if searching for "T" and no "T" is found, then the first row starting with "U" or any higher letter is returned. If there is no data following "T" at all, then the list size is returned. |
getSectionForPosition
open fun getSectionForPosition(position: Int): Int
Returns the section index for a given position in the list by querying the item and comparing it with all items in the section array.
Parameters | |
---|---|
position |
Int: the position within the adapter for which to return the corresponding section index |
Return | |
---|---|
Int |
the index of the corresponding section within the array of section objects, constrained to fall within the array bounds |
getSections
open fun getSections(): Array<Any!>!
Returns the section array constructed from the alphabet provided in the constructor.
Return | |
---|---|
Array<Any!>! |
the section array |
setCursor
open fun setCursor(cursor: Cursor!): Unit
Sets a new cursor as the data set and resets the cache of indices.
Parameters | |
---|---|
cursor |
Cursor!: the new cursor to use as the data set |
Protected methods
compare
protected open fun compare(
word: String!,
letter: String!
): Int
Default implementation compares the first character of word with letter.
Properties
mAlphabet
protected var mAlphabet: CharSequence!
The string of characters that make up the indexing sections.
mColumnIndex
protected var mColumnIndex: Int
The index of the cursor column that this list is sorted on.
mDataCursor
protected var mDataCursor: Cursor!
Cursor that is used by the adapter of the list view.