Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Same issue happens in my app when testing on the ICS tablets. It happens only when I have 11 elements in the list, when <strong>EndlessAdapter</strong> loads by 10 items at time.</p> <p>This is a race condition issue. The problem comes from the keepOnAppending flag update on the AsyncTask thread. While ListView accesses getCount on the UI thread andkeepOnAppending is set to true, later when it calls getView in the same UI thread andkeepOnAppending is already reset in the doInBackground:</p> <pre><code>@Override public int getCount() { if (keepOnAppending.get()) { return (super.getCount() + 1); // one more for // "pending" } return (super.getCount()); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (position == super.getCount() &amp;&amp; keepOnAppending.get()) { if (pendingView == null) { pendingView = getPendingView(parent); executeAsyncTask(new AppendTask()); } return (pendingView); } return (super.getView(position, convertView, parent)); } class AppendTask extends AsyncTask&lt;Void, Void, Exception&gt; { @Override protected Exception doInBackground(Void... params) { Exception result = null; try { keepOnAppending.set(cacheInBackground()); } catch (Exception e) { result = e; } return (result); } @Override protected void onPostExecute(Exception e) { if (e == null) { appendCachedData(); } else { keepOnAppending.set(onException(pendingView, e)); } pendingView = null; notifyDataSetChanged(); } } </code></pre> <p>As a fix, I changed the code of the AppendTask so that it updates keepOnAppending on the UI thread, only when new items added to the adapter.</p> <p>Here is the code:</p> <pre><code>class AppendTask extends AsyncTask&lt;Void, Void, AppendTask.Result&gt; { class Result { boolean status; Exception ex; public Result(Exception ex) { this.ex = ex; } public Result(boolean result) { this.status = result; } } @Override protected AppendTask.Result doInBackground(Void... params) { try { return new Result(cacheInBackground()); } catch (Exception e) { return new Result(e); } } @Override protected void onPostExecute(AppendTask.Result result) { if (result.ex == null) { appendCachedData(); keepOnAppending.set(result.status); } else { keepOnAppending.set(onException(pendingView, result.ex)); } pendingView = null; notifyDataSetChanged(); } } </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