我在 Android listview 中显示第一个字母的预览时遇到问题,当我快速滚动时,我得到了文本的预览,但它会指向列表中的错误位置。
例如,请看下图,现在我们在 M 部分,仍然出现 L 字母。
这是实现上述技术的Listadapter代码,代码有什么错误吗?
class MyListAdaptor extends ArrayAdapter<String> implements
SectionIndexer
{
HashMap<String, Integer> alphaIndexer;
String[] sections;
public MyListAdaptor(Context context, LinkedList<String> items) {
super(context, R.layout.list_item, items);
alphaIndexer = new HashMap<String, Integer>();
int size = items.size();
for (int x = 0; x < size; x++) {
String s = items.get(x);
// get the first letter of the store
String ch = s.substring(0, 1);
// convert to uppercase otherwise lowercase a -z will be sorted
// after upper A-Z
ch = ch.toUpperCase();
// HashMap will prevent duplicates
alphaIndexer.put(ch, x);
}
Set<String> sectionLetters = alphaIndexer.keySet();
// create a list from the set to sort
ArrayList<String> sectionList = new ArrayList<String>(
sectionLetters);
Collections.sort(sectionList);
sections = new String[sectionList.size()];
sectionList.toArray(sections);
}
public int getPositionForSection(int section) {
return alphaIndexer.get(sections[section]);
}
public int getSectionForPosition(int position) {
return 0;
}
public Object[] getSections() {
return sections;
}
}