android - AlphabetIndexer setCursor 不更新其缓存

标签 android android-listview android-cursor

我正在尝试使用 AlphabetIndexer 实现快速滚动条,但是当光标发生变化时,它不会刷新索引缓存。在我的 CursorAdapter 构造函数中,我调用了 setCursor(cursor) 但没有任何变化,并且根据文档:

Your adapter is responsible for updating the cursor by calling setCursor(Cursor) if the cursor changes. getPositionForSection(int) method does the binary search for the starting index of a given section (alphabet).

但是什么也没有发生。我将其用于搜索过滤器,因此当我搜索联系人时,它会使用联系人更新列表,因此 AlphabetIndexer 应该用于更新列表中新项目的索引。

示例:我的整个列表以“A”开头的联系人开头,以“E”开头的联系人结尾。所以 AlphabetIndexer 会在它的缓存中有这个索引。

但是,让我们尝试搜索以“C”开头的联系人,假设我有 250 个以“C”开头的联系人。因此,我必须快速滚动浏览此联系人,并且必须显示“C”索引,但它不只显示“C”,而是显示所有索引,这些索引是在我拥有整个列表时显示的。

这是我的 CursorAdapter 构造函数,我在其中为我键入的每个字母调用 setCursor(cursor):

public MyCursorAdapter(Context context, Cursor cursor, ArrayList<Integer> ids) 
        {
            super(context, cursor);
            try
            {
                mAlphaIndexer = new AlphabetIndexer(cursor, cursor.getColumnIndexOrThrow("Name")," ABCDEFGHIJKLMNOPQRSTUVWXYZ");
                notifyDataSetChanged(); 
                mAlphaIndexer.setCursor(cursor);        
                this.mSelectedContacts = ids;               
                Log.e("MyCursorAdapter", "construtor: ");
            }
            catch(Exception ex)
            {
                Log.e("MyCursorAdapter", "Error: " + ex);
            }
            finally
            {
                mAlphaIndexer.setCursor(cursor);
            }
        }

最佳答案

我通过在设置适配器后按顺序调用它解决了这个问题:

listView.setFastScrollEnabled(false);
listView.setFastScrollEnabled(true);

这对我有用。

关于android - AlphabetIndexer setCursor 不更新其缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6711178/

相关文章:

android - 如何从 Android 中的 .mov 文件中获取缩略图?

android - 使用 CursorLoader 和 LoaderManager 从 Android 应用程序中检索图像

java - 我该怎么做才能将日期发送到光标?

java - 这一行应该是什么? ( java )

android - 我可以为当前线程锁定 SQLite 表吗?

android - 如何取消 AOSP Nexus 构建的根目录?

android - 带标题/部分的抽屉导航

android - 无法获得项目位置以关闭列表项目

android - 对话框 fragment 问题 : onCreateDialog not called; can't get back key press

android - 不幸的是应用程序已停止,doinbackground() 出错