Android SectionIndexer接口不更新
我想制作一个带有字母顺序适配器的listview,以便在快速滚动时可以看到映射到每个部分的字母。初始化listview时,以下各项正常工作。但是,如果列表发生更改(例如添加或删除行),即使每次都创建了新适配器,索引器似乎也不会更新。它使用与原始列表相同的一组字母Android SectionIndexer接口不更新,android,listview,adapter,Android,Listview,Adapter,我想制作一个带有字母顺序适配器的listview,以便在快速滚动时可以看到映射到每个部分的字母。初始化listview时,以下各项正常工作。但是,如果列表发生更改(例如添加或删除行),即使每次都创建了新适配器,索引器似乎也不会更新。它使用与原始列表相同的一组字母 private void GenerateListView (final ArrayList<String> listItems) { try { listView = (ListV
private void GenerateListView (final ArrayList<String> listItems) {
try {
listView = (ListView) findViewById(R.id.listView_browser);
// generate section index adapter
AlphabeticalAdapter adapter = new AlphabeticalAdapter(this,
android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(adapter);
// recall scroll position
if (_currPos < listItems.size())
listView.setVerticalScrollbarPosition(_currPos);
} catch (Exception e) {
e.printStackTrace();
}
}
private void GenerateListView(最终ArrayList列表项){
试一试{
listView=(listView)findViewById(R.id.listView\u浏览器);
//生成节索引适配器
字母适配器=新字母适配器(此,
android.R.layout.simple_list_item_1,listItems);
setAdapter(适配器);
//调用滚动位置
如果(_currPos
下面是按字母顺序排列的适配器类
public class AlphabeticalAdapter extends ArrayAdapter<String> implements SectionIndexer
{
private HashMap<String, Integer> alphaIndexer;
private String[] sections;
public AlphabeticalAdapter(Context c, int resource, List<String> data)
{
// create ArrayAdapter<String>
super(c, resource, data);
// generate HashMap
alphaIndexer = new HashMap<>();
// generate index
for (int i = 0; i < data.size(); i++)
{
// convert first letter of each entry to upper case
String s = data.get(i).substring(0, 1).toUpperCase();
// map letter with corresponding index
if (!alphaIndexer.containsKey(s))
alphaIndexer.put(s, i);
}
// assign set view of keys in the HashMap
Set<String> sectionLetters = alphaIndexer.keySet();
// generate list from the set view
ArrayList<String> sectionList = new ArrayList<>(sectionLetters);
// sort list alphabetically
Collections.sort(sectionList);
// define and populate string array with the letters
sections = new String[sectionList.size()];
for (int i = 0; i < sectionList.size(); i++)
sections[i] = sectionList.get(i);
}
公共类AlphacalAdapter扩展ArrayAdapter实现SectionIndexer
{
私有HashMap字母索引器;
私有字符串[]节;
公共字母适配器(上下文c、int资源、列表数据)
{
//创建阵列适配器
超级(c、资源、数据);
//生成哈希映射
alphaIndexer=新HashMap();
//生成索引
对于(int i=0;i
看看这个解决方案
使用过滤器或任何数据更改来更新SectionIndexer将覆盖此方法。这对我来说是可行的
@Override
public void notifyDataSetInvalidated() {
//write your code
super.notifyDataSetInvalidated();
}
post complete stacktrace(即,您从何处获得该错误)我在适配器类末尾设置断点并观察变量时出错。抱歉,似乎我在10分钟前以某种方式修复了它,但此问题仍然存在。因此,它们似乎没有关联。