Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>Charsequence alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; </code></pre> <p>Here</p> <pre><code>A = 0 B = 1 C = 2 D = 3 E = 4 F = 5 . . . Z = 25 </code></pre> <p>Create a new AlphabetIndexer</p> <pre><code>AlphabetIndexer alphabetIndexer = new AlphabetIndexer(cursor, sortedColumnIndex, alphabets); </code></pre> <p>getSections() retreives the String[] of the alphabets that is provided to the alphabetIndexer</p> <pre><code>String[] sections = (String[]) alphabetIndexer.getSections(); </code></pre> <p>alphabetIndexer.getSectionForPosition(position) retreives the section for the current position</p> <p>Consider the given case</p> <pre><code>position Data getSectionForPosition(position) ________ _________ __________ 0 Ajdhfj j 0 1 Aadf hdsf 0 2 Ajfkldsahf 0 3 Asdhfa df 0 4 Badhf 1 5 Bdfj sadif 1 6 Bghoi ij 1 7 Bjkojg o 1 8 Cadj fkljsdf 2 9 Cgjds kfja 2 10 Cn khdfaj 2 11 Cph iohsdf 2 12 Czjfa sh 2 13 Dfgoa hjoifaj 3 14 Dzfjdak sfh 3 15 Fhf adhf 5 16 Zdfh ajdf 25 </code></pre> <p>To get the section for the position </p> <pre><code>String section = sections[getSectionForPosition(position)]; </code></pre> <p>alphabetIndexer.getPositionForSection(section) retreives the first position at which the data starts with that section</p> <pre><code>section getPositionForSection(section) _________ ____________________ 0 0 1 4 2 8 3 13 4 13 5 15 6 15 7 15 8 15 . . . . . . 23 15 24 15 25 16 </code></pre> <p>Hope this helps you.</p> <p>An example of using alphabetIndexer</p> <pre><code>public abstract class SectionedListAdapter extends ResourceCursorAdapter { private SparseIntArray sections; private SparseIntArray cursorPositions; private Context mContext; private int sortedColumnIndex; private static final int NORMAL_LIST_VIEW = 0; private static final int SECTION_LIST_VIEW = 1; private static final int NULL_LIST_VIEW = 2; public SectionedListAdapter(Context context, int layout, Cursor c, boolean autoRequery, int sortedColumnIndex, int defaultBitmapResId) { super(context, layout, c, autoRequery); this.mContext = context; this.sortedColumnIndex = sortedColumnIndex; setSortedCursorColumn(c); } public void setSortedCursorColumn(Cursor cursor){ if (cursor == null){ return; } AlphabetIndexer alphabetIndexer = new AlphabetIndexer(cursor, sortedColumnIndex, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); sections = new SparseIntArray(); int m=0; int t = 'A'; for (int i=0; i &lt; 26; i++){ if ((i+1) &lt; 26) { int position = alphabetIndexer.getPositionForSection(i); int temp = alphabetIndexer.getPositionForSection(i + 1); if (temp != position){ sections.put(position + m, t+i); m++; } } else { int position = alphabetIndexer.getPositionForSection(i); int temp = alphabetIndexer.getPositionForSection(i-1); if (position != temp){ sections.put(position + m, t+i); } } } int temp = 0; cursorPositions = new SparseIntArray(); for (int i=0; i&lt;cursor.getCount() + sections.size(); i++){ if (sections.get(i, -1) != -1){ temp ++; cursorPositions.put(i, -1); } else { cursorPositions.put(i, i - temp); } } } @Override public View getView(int position, View view, ViewGroup parent) { if (getItemViewType(position) == NORMAL_LIST_VIEW){ if (view == null){ view = newView(mContext, getCursor(), parent); } getCursor().moveToPosition(cursorPositions.get(position)); bindView(view, mContext, getCursor(), position); } else if (getItemViewType(position) == SECTION_LIST_VIEW) { if (view == null){ view = newSectionView(mContext, getCursor(), parent); } bindSectionView(view, mContext, (char)sections.get(position)); } else { if (view == null){ } else { view = new ViewStub(mContext); } } return view; } public abstract View newSectionView(Context context, Cursor cursor, ViewGroup parent); public abstract void bindSectionView(View view, Context context, char text); public abstract void bindView(View view, Context context, Cursor cursor, int position); @Override public boolean areAllItemsEnabled() { return false; } @Override public boolean isEnabled(int position) { return cursorPositions.get(position) &gt;= 0; } @Override public int getItemViewType(int position) { if (cursorPositions.get(position) &gt;= 0){ return NORMAL_LIST_VIEW; }else if (cursorPositions.get(position) == -1){ return SECTION_LIST_VIEW; } else { return NULL_LIST_VIEW; } } @Override public int getViewTypeCount() { return 3; } private void putCursorExclusion(int position, boolean isActualPosition){ if (isActualPosition) { cursorPositions.put(position, -cursorPositions.get(position)); } else { int actualPosition = cursorPositions.keyAt(cursorPositions.indexOfValue(position)); cursorPositions.put(actualPosition, -position); } } private void removeCursorExclusion(int position, boolean isActualPosition){ if (isActualPosition){ cursorPositions.put(position, -cursorPositions.get(position)); } else { int actualPosition = cursorPositions.keyAt(cursorPositions.indexOfValue(-position)); cursorPositions.put(actualPosition, position); } } @Override public int getCount() { return cursorPositions == null ? 0 : cursorPositions.size(); } public SparseIntArray getSections() { return sections; } public SparseIntArray getCursorPositions() { return cursorPositions; } @Override public void bindView(View view, Context context, Cursor cursor) { //do nothing here } @Override public Cursor swapCursor(Cursor newCursor) { return super.swapCursor(newCursor); } @Override public void notifyDataSetChanged() { super.notifyDataSetChanged(); } @Override public void changeCursor(Cursor cursor) { setSortedCursorColumn(cursor); super.changeCursor(cursor); } } </code></pre> <p>To use this just create a new class extends SectionedListAdapter and then provide required details.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload