AlphabetIndexer


public class AlphabetIndexer
extends DataSetObserver implements SectionIndexer

java.lang.Object
   ↳ 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 calling setCursor(Cursor) if the cursor changes. getPositionForSection(int) method does the binary search for the starting index of a given section (alphabet).

Summary

Fields

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.

Public constructors

AlphabetIndexer(Cursor cursor, int sortedColumnIndex, CharSequence alphabet)

Constructs the indexer.

Public methods

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 methods

int compare(String word, String letter)

Default implementation compares the first character of word with letter.

Inherited methods

Fields

mAlphabet

Added in API level 3
protected CharSequence mAlphabet

The string of characters that make up the indexing sections.

mColumnIndex

Added in API level 3
protected int mColumnIndex

The index of the cursor column that this list is sorted on.

mDataCursor

Added in API level 3
protected Cursor mDataCursor

Cursor that is used by the adapter of the list view.

Public constructors

AlphabetIndexer

Added in API level 3
public AlphabetIndexer (Cursor cursor, 
                int sortedColumnIndex, 
                CharSequence alphabet)

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

Added in API level 3
public int getPositionForSection (int sectionIndex)

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

Returns
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

Added in API level 3
public 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.

Parameters
position int: the position within the adapter for which to return the corresponding section index

Returns
int the index of the corresponding section within the array of section objects, constrained to fall within the array bounds

getSections

Added in API level 3
public Object[] getSections ()

Returns the section array constructed from the alphabet provided in the constructor.

Returns
Object[] the section array

onChanged

Added in API level 3
public void onChanged ()

This method is called when the entire data set has changed, most likely through a call to Cursor#requery() on a Cursor.

onInvalidated

Added in API level 3
public 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.

setCursor

Added in API level 3
public void setCursor (Cursor cursor)

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

Added in API level 3
protected int compare (String word, 
                String letter)

Default implementation compares the first character of word with letter.

Parameters
word String

letter String

Returns
int