Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid refresh ListView sections-overlay not working in 4.4
    text
    copied!<p>Hi Stackoverflow community,</p> <p>I am currently playing around with Android ListViewsections capabilties, but there seems to be an issue in Android 4.4 API. I have a CursorAdapter which populates the items from a SQLite database. When the adapter gets created, it sections his items. The user has the posssiblity to change the sorting of the ListView items from A-Z to Z-A. When he does this, a new CursorAdapter will be created and set, which of course indexes his items again. I used the following code to set the new Adapter, which seemed to force the Listview to call getSections() and refreshed the sections when fast scrolling:</p> <pre><code>//A new instance of adapter is created here with the new cursor, see code at bottom listView.setFastScrollEnabled(false); listView.setAdapter(adapter); listView.setFastScrollEnabled(true); </code></pre> <p>The above code works great up to Android 4.3, but stopped working in 4.4. In 4.4, the ListView shows the old sections while fast scrolling: So, when the user changed the sorting to Z-A, the ListView still shows this sections-overlay in order A-Z when fast scrolling. I hope you know what I mean.</p> <p>Is this a known bug and is there a workaround for this?</p> <p>Thank you very much.</p> <p>EDIT: This is the code of the adapter I use. It is based on basically 2 cursors. The first one holds the data to show and the second one the indices.</p> <pre><code>public class SectionCheckableCursorAdapter extends CursorAdapter implements SectionIndexer { private LayoutInflater mInflater; private Map&lt;Integer, Object&gt; mSectionIndices = new TreeMap&lt;Integer, Object&gt;(); private Object[] mSections; private Integer[] mSectionPositions; public SectionCheckableCursorAdapter(Context context, Cursor c, Cursor indexCursor) { super(context, c, false); mInflater = LayoutInflater.from(context); buildIndex(indexCursor); } private void buildIndex(Cursor indexCursor) { //init mSectionIndices = new TreeMap&lt;Integer, Object&gt;(); List&lt;Integer&gt; sectionPositionsList = new ArrayList&lt;Integer&gt;(); //get column indices from cursor int indexFirstLetter = indexCursor.getColumnIndex("firstLetter"); int indexCount = indexCursor.getColumnIndex("count"); //build sections int currentCount = 0; while (indexCursor.moveToNext()) { final int count = indexCursor.getInt(indexCount); String firstChar = indexCursor.getString(indexFirstLetter); if (firstChar == null || firstChar.isEmpty()) { firstChar = " "; } if (StringUtils.isNumber(firstChar)) { firstChar = "#"; } if (!mSectionIndices.containsValue(firstChar)) { mSectionIndices.put(currentCount, firstChar); sectionPositionsList.add(currentCount); } currentCount += count; } mSectionPositions = new Integer[sectionPositionsList.size()]; sectionPositionsList.toArray(mSectionPositions); Collection&lt;Object&gt; sections_collection = mSectionIndices.values(); mSections = new Object[sections_collection.size()]; sections_collection.toArray(mSections); } @Override public int getPositionForSection(int section) { if (section &gt; mSectionPositions.length - 1) { if (getCursor() != null) return getCursor().getCount() - 1; return 0; } else { return mSectionPositions[section]; } } @Override public int getSectionForPosition(int position) { for (int i = 0; i &lt; mSectionPositions.length-1; i++) { if (position &gt;= mSectionPositions[i] &amp;&amp; position &lt; mSectionPositions[i+1]) { return i; } } return mSectionPositions.length-1; } @Override public Object[] getSections() { return mSections; } </code></pre> <p>}</p> <p>This is how the Cursors and Adapter are created, before the Adapter gets set like in the code at top:</p> <pre><code>//Query for "real" data and the indices. The indices are created with a query because //this is much faster than iterating through all entries in the "data" cursor mCursor = mDatabase.rawQuery(query, null); mIndexCursor = mDatabase.rawQuery(indexQuery, null); if (mCursor != null) { adapter = new SectionCheckableCursorAdapter(getSherlockActivity(), mCursor, mIndexCursor); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload