Added in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Fields](https://developer.android.com/reference/android/widget/AlphabetIndexer#lfields) \| [Ctors](https://developer.android.com/reference/android/widget/AlphabetIndexer#pubctors) \| [Methods](https://developer.android.com/reference/android/widget/AlphabetIndexer#pubmethods) \| [Protected Methods](https://developer.android.com/reference/android/widget/AlphabetIndexer#promethods) \| [Inherited Methods](https://developer.android.com/reference/android/widget/AlphabetIndexer#inhmethods)

# AlphabetIndexer

*** ** * ** ***

` public class AlphabetIndexer `   
` ` ` extends https://developer.android.com/reference/android/database/DataSetObserver ` ` implements https://developer.android.com/reference/android/widget/SectionIndexer `

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) |||
| ↳ | [android.database.DataSetObserver](https://developer.android.com/reference/android/database/DataSetObserver) ||
|   | ↳ | android.widget.AlphabetIndexer |

<br />

*** ** * ** ***

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.

<br />

Your adapter is responsible for updating the cursor by calling `https://developer.android.com/reference/android/widget/AlphabetIndexer#setCursor(android.database.Cursor)` if the cursor changes. `https://developer.android.com/reference/android/widget/AlphabetIndexer#getPositionForSection(int)` method does the binary search for the starting index of a given section (alphabet).

<br />

## Summary

| ### Fields ||
|---|---|
| ` protected https://developer.android.com/reference/java/lang/CharSequence` | `https://developer.android.com/reference/android/widget/AlphabetIndexer#mAlphabet` The string of characters that make up the indexing sections. |
| ` protected int` | `https://developer.android.com/reference/android/widget/AlphabetIndexer#mColumnIndex` The index of the cursor column that this list is sorted on. |
| ` protected https://developer.android.com/reference/android/database/Cursor` | `https://developer.android.com/reference/android/widget/AlphabetIndexer#mDataCursor` Cursor that is used by the adapter of the list view. |

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/widget/AlphabetIndexer#AlphabetIndexer(android.database.Cursor,%20int,%20java.lang.CharSequence)(https://developer.android.com/reference/android/database/Cursor cursor, int sortedColumnIndex, https://developer.android.com/reference/java/lang/CharSequence alphabet) ` Constructs the indexer. |

| ### Public methods ||
|---|---|
| ` int` | ` https://developer.android.com/reference/android/widget/AlphabetIndexer#getPositionForSection(int)(int sectionIndex) ` Performs a binary search or cache lookup to find the first row that matches a given section's starting letter. |
| ` int` | ` https://developer.android.com/reference/android/widget/AlphabetIndexer#getSectionForPosition(int)(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. |
| ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/android/widget/AlphabetIndexer#getSections()() ` Returns the section array constructed from the alphabet provided in the constructor. |
| ` void` | ` https://developer.android.com/reference/android/widget/AlphabetIndexer#onChanged()() ` This method is called when the entire data set has changed, most likely through a call to `https://developer.android.com/reference/android/database/Cursor#requery()` on a `https://developer.android.com/reference/android/database/Cursor`. |
| ` void` | ` https://developer.android.com/reference/android/widget/AlphabetIndexer#onInvalidated()() ` This method is called when the entire data becomes invalid, most likely through a call to `https://developer.android.com/reference/android/database/Cursor#deactivate()` or `https://developer.android.com/reference/android/database/Cursor#close()` on a `https://developer.android.com/reference/android/database/Cursor`. |
| ` void` | ` https://developer.android.com/reference/android/widget/AlphabetIndexer#setCursor(android.database.Cursor)(https://developer.android.com/reference/android/database/Cursor cursor) ` Sets a new cursor as the data set and resets the cache of indices. |

| ### Protected methods ||
|---|---|
| ` int` | ` https://developer.android.com/reference/android/widget/AlphabetIndexer#compare(java.lang.String,%20java.lang.String)(https://developer.android.com/reference/java/lang/String word, https://developer.android.com/reference/java/lang/String letter) ` Default implementation compares the first character of word with letter. |

### Inherited methods

From class ` https://developer.android.com/reference/android/database/DataSetObserver ` ` void` ` https://developer.android.com/reference/android/database/DataSetObserver#onChanged()() `

This method is called when the entire data set has changed, most likely through a call to `https://developer.android.com/reference/android/database/Cursor#requery()` on a `https://developer.android.com/reference/android/database/Cursor`.
` void` ` https://developer.android.com/reference/android/database/DataSetObserver#onInvalidated()() `

This method is called when the entire data becomes invalid, most likely through a call to `https://developer.android.com/reference/android/database/Cursor#deactivate()` or `https://developer.android.com/reference/android/database/Cursor#close()` on a `https://developer.android.com/reference/android/database/Cursor`.
From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.
From interface ` https://developer.android.com/reference/android/widget/SectionIndexer ` ` abstract int` ` https://developer.android.com/reference/android/widget/SectionIndexer#getPositionForSection(int)(int sectionIndex) `

Given the index of a section within the array of section objects, returns the starting position of that section within the adapter.
` abstract int` ` https://developer.android.com/reference/android/widget/SectionIndexer#getSectionForPosition(int)(int position) `

Given a position within the adapter, returns the index of the corresponding section within the array of section objects.
` abstract https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/android/widget/SectionIndexer#getSections()() `

Returns an array of objects representing sections of the list.

## Fields

### mAlphabet

Added in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected CharSequence mAlphabet
```

The string of characters that make up the indexing sections.

<br />

### mColumnIndex

Added in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected int mColumnIndex
```

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

<br />

### mDataCursor

Added in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected Cursor mDataCursor
```

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

<br />

## Public constructors

### AlphabetIndexer

Added in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public AlphabetIndexer (Cursor cursor, 
                int sortedColumnIndex, 
                CharSequence alphabet)
```

Constructs the indexer.

<br />

| Parameters ||
|---|---|
| `cursor` | `Cursor`: the cursor containing the data set <br /> |
| `sortedColumnIndex` | `int`: the column number in the cursor that is sorted alphabetically <br /> |
| `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. <br /> |

## Public methods

### getPositionForSection

Added in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Parameters ||
|---|---|
| `sectionIndex` | `int`: the section to search for <br /> |

| 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. <br /> |

### getSectionForPosition

Added in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Parameters ||
|---|---|
| `position` | `int`: the position within the adapter for which to return the corresponding section index <br /> |

| Returns ||
|---|---|
| `int` | the index of the corresponding section within the array of section objects, constrained to fall within the array bounds <br /> |

### getSections

Added in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Object[] getSections ()
```

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

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/Object` | the section array <br /> |

### onChanged

Added in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onChanged ()
```

This method is called when the entire data set has changed, most likely through a call to `https://developer.android.com/reference/android/database/Cursor#requery()` on a `https://developer.android.com/reference/android/database/Cursor`.

<br />

### onInvalidated

Added in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onInvalidated ()
```

This method is called when the entire data becomes invalid, most likely through a call to `https://developer.android.com/reference/android/database/Cursor#deactivate()` or `https://developer.android.com/reference/android/database/Cursor#close()` on a `https://developer.android.com/reference/android/database/Cursor`.

<br />

### setCursor

Added in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setCursor (Cursor cursor)
```

Sets a new cursor as the data set and resets the cache of indices.

<br />

| Parameters ||
|---|---|
| `cursor` | `Cursor`: the new cursor to use as the data set <br /> |

## Protected methods

### compare

Added in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected int compare (String word, 
                String letter)
```

Default implementation compares the first character of word with letter.

<br />

| Parameters ||
|---|---|
| `word` | `String` <br /> |
| `letter` | `String` <br /> |

| Returns ||
|---|---|
| `int` | <br /> |