| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.database.Cursor; |
| 20 |
import android.database.DataSetObserver; |
| 21 |
import android.util.SparseIntArray; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class AlphabetIndexer extends DataSetObserver implements SectionIndexer { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
protected Cursor mDataCursor; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
protected int mColumnIndex; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
protected CharSequence mAlphabet; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
private int mAlphabetLength; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
private SparseIntArray mAlphaMap; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
private java.text.Collator mCollator; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private String[] mAlphabetArray; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public AlphabetIndexer(Cursor cursor, int sortedColumnIndex, CharSequence alphabet) { |
| 83 |
mDataCursor = cursor; |
| 84 |
mColumnIndex = sortedColumnIndex; |
| 85 |
mAlphabet = alphabet; |
| 86 |
mAlphabetLength = alphabet.length(); |
| 87 |
mAlphabetArray = new String[mAlphabetLength]; |
| 88 |
for (int i = 0; i < mAlphabetLength; i++) { |
| 89 |
mAlphabetArray[i] = Character.toString(mAlphabet.charAt(i)); |
| 90 |
} |
| 91 |
mAlphaMap = new SparseIntArray(mAlphabetLength); |
| 92 |
if (cursor != null) { |
| 93 |
cursor.registerDataSetObserver(this); |
| 94 |
} |
| 95 |
|
| 96 |
mCollator = java.text.Collator.getInstance(); |
| 97 |
mCollator.setStrength(java.text.Collator.PRIMARY); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public Object[] getSections() { |
| 105 |
return mAlphabetArray; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public void setCursor(Cursor cursor) { |
| 113 |
if (mDataCursor != null) { |
| 114 |
mDataCursor.unregisterDataSetObserver(this); |
| 115 |
} |
| 116 |
mDataCursor = cursor; |
| 117 |
if (cursor != null) { |
| 118 |
mDataCursor.registerDataSetObserver(this); |
| 119 |
} |
| 120 |
mAlphaMap.clear(); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
protected int compare(String word, String letter) { |
| 127 |
final String firstLetter; |
| 128 |
if (word.length() == 0) { |
| 129 |
firstLetter = " "; |
| 130 |
} else { |
| 131 |
firstLetter = word.substring(0, 1); |
| 132 |
} |
| 133 |
|
| 134 |
return mCollator.compare(firstLetter, letter); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public int getPositionForSection(int sectionIndex) { |
| 147 |
final SparseIntArray alphaMap = mAlphaMap; |
| 148 |
final Cursor cursor = mDataCursor; |
| 149 |
|
| 150 |
if (cursor == null || mAlphabet == null) { |
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
if (sectionIndex <= 0) { |
| 156 |
return 0; |
| 157 |
} |
| 158 |
if (sectionIndex >= mAlphabetLength) { |
| 159 |
sectionIndex = mAlphabetLength - 1; |
| 160 |
} |
| 161 |
|
| 162 |
int savedCursorPos = cursor.getPosition(); |
| 163 |
|
| 164 |
int count = cursor.getCount(); |
| 165 |
int start = 0; |
| 166 |
int end = count; |
| 167 |
int pos; |
| 168 |
|
| 169 |
char letter = mAlphabet.charAt(sectionIndex); |
| 170 |
String targetLetter = Character.toString(letter); |
| 171 |
int key = letter; |
| 172 |
|
| 173 |
if (Integer.MIN_VALUE != (pos = alphaMap.get(key, Integer.MIN_VALUE))) { |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
if (pos < 0) { |
| 178 |
pos = -pos; |
| 179 |
end = pos; |
| 180 |
} else { |
| 181 |
|
| 182 |
return pos; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
if (sectionIndex > 0) { |
| 188 |
int prevLetter = |
| 189 |
mAlphabet.charAt(sectionIndex - 1); |
| 190 |
int prevLetterPos = alphaMap.get(prevLetter, Integer.MIN_VALUE); |
| 191 |
if (prevLetterPos != Integer.MIN_VALUE) { |
| 192 |
start = Math.abs(prevLetterPos); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
pos = (end + start) / 2; |
| 199 |
|
| 200 |
while (pos < end) { |
| 201 |
|
| 202 |
cursor.moveToPosition(pos); |
| 203 |
String curName = cursor.getString(mColumnIndex); |
| 204 |
if (curName == null) { |
| 205 |
if (pos == 0) { |
| 206 |
break; |
| 207 |
} else { |
| 208 |
pos--; |
| 209 |
continue; |
| 210 |
} |
| 211 |
} |
| 212 |
int diff = compare(curName, targetLetter); |
| 213 |
if (diff != 0) { |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
if (diff < 0) { |
| 226 |
start = pos + 1; |
| 227 |
if (start >= count) { |
| 228 |
pos = count; |
| 229 |
break; |
| 230 |
} |
| 231 |
} else { |
| 232 |
end = pos; |
| 233 |
} |
| 234 |
} else { |
| 235 |
|
| 236 |
if (start == pos) { |
| 237 |
|
| 238 |
break; |
| 239 |
} else { |
| 240 |
|
| 241 |
end = pos; |
| 242 |
} |
| 243 |
} |
| 244 |
pos = (start + end) / 2; |
| 245 |
} |
| 246 |
alphaMap.put(key, pos); |
| 247 |
cursor.moveToPosition(savedCursorPos); |
| 248 |
return pos; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public int getSectionForPosition(int position) { |
| 256 |
int savedCursorPos = mDataCursor.getPosition(); |
| 257 |
mDataCursor.moveToPosition(position); |
| 258 |
String curName = mDataCursor.getString(mColumnIndex); |
| 259 |
mDataCursor.moveToPosition(savedCursorPos); |
| 260 |
|
| 261 |
|
| 262 |
for (int i = 0; i < mAlphabetLength; i++) { |
| 263 |
char letter = mAlphabet.charAt(i); |
| 264 |
String targetLetter = Character.toString(letter); |
| 265 |
if (compare(curName, targetLetter) == 0) { |
| 266 |
return i; |
| 267 |
} |
| 268 |
} |
| 269 |
return 0; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
@Override |
| 276 |
public void onChanged() { |
| 277 |
super.onChanged(); |
| 278 |
mAlphaMap.clear(); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
@Override |
| 285 |
public void onInvalidated() { |
| 286 |
super.onInvalidated(); |
| 287 |
mAlphaMap.clear(); |
| 288 |
} |
| 289 |
} |