private class MyAdapter extends SimpleCursorAdapter implements SectionIndexer {
private MusicAlphaIndexer indexer;
private String index = "ABCDEFGHIJKLMNOPQRSTUVWXYZ*";
Map<Integer, Integer> pos = new HashMap<Integer, Integer>();
public MyAdapter(Context context, int layout, Cursor c, String[] from,
int[] to) {
super(context, layout, c, from, to);
indexer = new MusicAlphaIndexer(c, 1, index);
}
@Override
public int getPositionForSection(int section) {
return indexer.getPositionForSection(section);
}
@Override
public int getSectionForPosition(int position) {
Log.d("tag", "getSectionForPosition " + indexer.getSectionForPosition(position));
// return indexer.getSectionForPosition(position);
return 0;
}
@Override
public Object[] getSections() {
return indexer.getSections();
}
}
private class MusicAlphaIndexer extends AlphabetIndexer {
public MusicAlphaIndexer(Cursor cursor, int sortedColumnIndex,
CharSequence alphabet) {
super(cursor, sortedColumnIndex, alphabet);
}
@Override
protected int compare(String word, String letter) {
String key1 = Audio.keyFor(word);
String key2 = Audio.keyFor(letter);
if(key2.startsWith(key1)) {
return 0;
}
return key1.compareTo(key2);
}
}
1。必须要重写AlphabetIndexer中的compare
2。Audio.KeyFor();是为根据一定的规则,生成一个用户不可读的串,进行比较
3。最后没有被匹配的数据,都属于最后一个,这里是"*"
4。要设置listview.setFastScrollEnabled(true);
5。数据不够,不会显示快速的滑块