public class AlphabetIndexer extends DataSetObserver implements SectionIndexer
setCursor(android.database.Cursor)
if the
cursor changes. getPositionForSection(int)
method does the binary search for the starting
index of a given section (alphabet).Modifier and Type | Field and Description |
---|---|
protected CharSequence |
mAlphabet
The string of characters that make up the indexing sections.
|
protected int |
mColumnIndex
The index of the cursor column that this list is sorted on.
|
protected Cursor |
mDataCursor
Cursor that is used by the adapter of the list view.
|
Constructor and Description |
---|
AlphabetIndexer(Cursor cursor,
int sortedColumnIndex,
CharSequence alphabet)
Constructs the indexer.
|
Modifier and Type | Method and Description |
---|---|
protected int |
compare(String word,
String letter)
Default implementation compares the first character of word with letter.
|
int |
getPositionForSection(int sectionIndex)
Performs a binary search or cache lookup to find the first row that
matches a given section's starting letter.
|
int |
getSectionForPosition(int position)
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.
|
Object[] |
getSections()
Returns the section array constructed from the alphabet provided in the constructor.
|
void |
onChanged()
This method is called when the entire data set has changed,
most likely through a call to
Cursor.requery() on a Cursor . |
void |
onInvalidated()
This method is called when the entire data becomes invalid,
most likely through a call to
Cursor.deactivate() or Cursor.close() on a
Cursor . |
void |
setCursor(Cursor cursor)
Sets a new cursor as the data set and resets the cache of indices.
|
protected Cursor mDataCursor
protected int mColumnIndex
protected CharSequence mAlphabet
public AlphabetIndexer(Cursor cursor, int sortedColumnIndex, CharSequence alphabet)
cursor
- the cursor containing the data setsortedColumnIndex
- the column number in the cursor that is sorted
alphabeticallyalphabet
- 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 Object[] getSections()
getSections
in interface SectionIndexer
public void setCursor(Cursor cursor)
cursor
- the new cursor to use as the data setprotected int compare(String word, String letter)
public int getPositionForSection(int sectionIndex)
getPositionForSection
in interface SectionIndexer
sectionIndex
- the section to search forpublic int getSectionForPosition(int position)
getSectionForPosition
in interface SectionIndexer
position
- the position within the adapter for which to return the
corresponding section indexpublic void onChanged()
DataSetObserver
Cursor.requery()
on a Cursor
.onChanged
in class DataSetObserver
public void onInvalidated()
DataSetObserver
Cursor.deactivate()
or Cursor.close()
on a
Cursor
.onInvalidated
in class DataSetObserver