Search code examples
androidandroid-listviewandroid-cursor

AlphabetIndexer setCursor doesn't update its cache


I'm trying to implement a fast scroller with AlphabetIndexer, but when the cursor changes, its not refreshing the index chache. In my CursorAdapter constructor I call setCursor(cursor) but nothing changes, and acording to the documentation:

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).

But nothing is happening. I'm using this for a search filter, so when I search contacts, it updates the list with the contacts, so AlphabetIndexer should work for update the index of the new items in the list.

Example: My whole list starts with contacts that starts with 'A' and ends with contacts that starts with 'E'. So the AlphabetIndexer will have this indexes in its cache.

But, lets try to search contacts with 'C', and lets say I have 250 contacts that starts with 'C'. So, I have to fast scroll through this contacts, and the 'C' index must show, but instead of only 'C' it shows all indexes, that were shown when I had the entire list.

Here is my CursorAdapter constructor where I call setCursor(cursor) for every letter I type:

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);
            }
        }

Solution

  • I've solved this by calling this in sequence after I set the adapter:

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

    That worked for me.