androidlistviewfastscroll

ListView with AlphaIndexer Cache Not Clearing


So I have one activity with a listview in it that is dynamically updated via cursors. The cursor object is being reused by simply reassigning a query command to this variable, which returns a whole new set of data. This works fine. The issue is that I have extended SimpleCursorAdapter to work with an AlphaIndexer. Apparently when the cursor is updated or changed, its supposed to clear the indexed cache. This is not happening. The main reason for all of this is to have fast scroll working on different cursors that are passed in and have it work. Right now, in using different cursors, the listview is using indexes from the first listview with trying to fastscroll through the second listview.

class AlphaCursor extends SimpleCursorAdapter implements SectionIndexer {

    AlphabetIndexer alphaIndexer;
    private int list_type;
    public AlphaCursor(Context context, int layout, Cursor c, String[] from, int[] to, int type, String sortBy) {
        super(context, layout, c, from, to);
        // MUST have space in front of alphabet
        int count = c.getCount();
        // this.onContentChanged();doesnt do a thing
        alphaIndexer = new AlphabetIndexer(c, c.getColumnIndex(sortBy), " ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        list_type = type;

Any idea what might be going on or how to clear this cache? I tried onChanged() as well as onContentChanged(). Has anyone seen this or know any suggestions?

The code is being used like such:

    alpha = new AlphaCursor(ClassActivity.this, R.layout.list_item, m_cursor, from, to, TAB_HOME, "name");
    alpha.changeCursor(m_cursor);
mList.setAdapter(alpha);

Keep in mind, I have 4 'tabs' that just requery a cursor and create a new AlphaIndexer. Each time a tab is clicked, the alpha variable is nulled out. It looks like there is view cahcing with the indexer.

Thanks


Solution

  • Since it seems that the AlphaIndexer becomes tied to the ScrollView when it's first used, it ignores any subsequent changes when a new one is set in the Adapter.

    To get around the problem as described in the question, I made the AlphaIndexer a member of the ListFragment and created it once. On subsequent changes to the cursor used in the adapter, I just called alphaIndexer.setCursor(aCursor). This fixed the 'stale' index problem and also the 'offset' alphabet when trying to use setFastScrollEnabled() with false/true.

    I assume that this will also work with a ListActivity.