The code in sideindex-for-android is more complex than it should be.
LinearLayout, , TextView . .
onClick() , .
, . setSelection() .
public static final String alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public Map<String, Integer> getAlphabetOffsets() {
HashMap<String, Integer> map = new HashMap<>();
for (int idx = 0; idx < alphabet.length(); idx++) {
String aLetter = alphabet.substring(idx, idx+1);
map.put(aLetter, -1);
}
int numFound = cursor.getCount();
cursor.moveToFirst();
for (int idx = 0; idx < numFound; idx++) {
String productName = cursor.getString(cursor.getColumnIndex(DB.PRODUCTS_NAME_COL));
String current;
if (productName == null || productName.equals("")) {
current = "0";
} else {
current = productName.substring(0, 1).toUpperCase();
}
if (map.containsKey(current) && map.get(current) < 0)
map.put(current, idx);
cursor.moveToNext();
}
map.put("0", 0);
int lastFound = 0;
for (int idx = 0; idx < alphabet.length(); idx++) {
String current = alphabet.substring(idx, idx+1);
if ( map.get(current) < 0 ) {
map.put(current, lastFound);
} else {
lastFound = map.get(current);
}
System.out.println("alphabet \"" + current + "\" = " + map.get(current));
}
return map;
}