Note that there are some explanatory texts on larger screens.

plurals
  1. POThe content of the adapter has changed but ListView did not receive a notification - With AsyncTask
    primarykey
    data
    text
    <p>I am using AsyncTask to set my items and stuff, in the postExecute i call a synchronized Method where i do this </p> <pre><code>mAllResultsAdapter.setItems(mAllResultsItem); mAllResultsAdapter.notifyDataSetChanged(); mListView.invalidate(); mListView.requestLayout(); </code></pre> <p>Most of the time if I dowload new data and update my listview/adapter it simply works, and pretty good to. But somehow it crashes sometime, can anyone assist me with this issue.</p> <p>heres my crash log.</p> <pre><code> 03-19 14:15:02.170: E/AndroidRuntime(23242): FATAL EXCEPTION: main 03-19 14:15:02.170: E/AndroidRuntime(23242): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131034153, class android.widget.ListView) with Adapter(class com....tracebuzz.allresults.AllResultsAdapter)] 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.widget.ListView.layoutChildren(ListView.java:1556) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.widget.AbsListView.onLayout(AbsListView.java:1993) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.View.layout(View.java:9606) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.ViewGroup.layout(ViewGroup.java:3879) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.widget.FrameLayout.onLayout(FrameLayout.java:400) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.View.layout(View.java:9606) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.ViewGroup.layout(ViewGroup.java:3879) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.View.layout(View.java:9606) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.ViewGroup.layout(ViewGroup.java:3879) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.widget.FrameLayout.onLayout(FrameLayout.java:400) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.View.layout(View.java:9606) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.ViewGroup.layout(ViewGroup.java:3879) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1542) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1403) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.widget.LinearLayout.onLayout(LinearLayout.java:1314) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.View.layout(View.java:9606) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.ViewGroup.layout(ViewGroup.java:3879) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.widget.FrameLayout.onLayout(FrameLayout.java:400) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.View.layout(View.java:9606) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.ViewGroup.layout(ViewGroup.java:3879) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.ViewRoot.performTraversals(ViewRoot.java:1288) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.view.ViewRoot.handleMessage(ViewRoot.java:2066) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.os.Handler.dispatchMessage(Handler.java:99) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.os.Looper.loop(Looper.java:132) 03-19 14:15:02.170: E/AndroidRuntime(23242): at android.app.ActivityThread.main(ActivityThread.java:4126) 03-19 14:15:02.170: E/AndroidRuntime(23242): at java.lang.reflect.Method.invokeNative(Native Method) 03-19 14:15:02.170: E/AndroidRuntime(23242): at java.lang.reflect.Method.invoke(Method.java:491) 03-19 14:15:02.170: E/AndroidRuntime(23242): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844) 03-19 14:15:02.170: E/AndroidRuntime(23242): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 03-19 14:15:02.170: E/AndroidRuntime(23242): at dalvik.system.NativeStart.main(Native Method) </code></pre> <p>I Should update my qeustion with my code, so here it is.</p> <pre><code>public class AllResultsActivity extends TBActivity{ public String identifier, feedIdentifier, apiKey, tagID, titleName; private ProcesReuest procesReuest; public ArrayList&lt;Integer&gt; tempIDHolder = new ArrayList&lt;Integer&gt;(); public ArrayList&lt;AllResultsItem&gt; mAllResultsItem; public AllResultsAdapter mAllResultsAdapter; public ListView mListView; private Context mContext; private ViewFlipper mViewFlipper; private TextView mTitleText; private int itemsForLoad = 50; private boolean firstRun = true; private ProgressBar mLoadItemsIndicator; private int visibleThreshold = 5; private int mPage = 1; private int previousTotal = 0; private boolean loading = true; private SharedPreferences mSettings; private Editor mEdit; private View footerView; private int itemHolder; @Override public void onBackPressed() { super.onBackPressed(); procesReuest.cancel(true); AllResultsActivity.this.finish(); } @Override public void onCreate(Bundle savedInstanceState) { this.requestWindowFeature(Window.FEATURE_NO_TITLE); this.setContentView(R.layout.all_results_view); super.onCreate(savedInstanceState); getExtras(); mContext = this; itemsForLoad = 50; loadItems(apiKey); initListview(); } private void initListview() { mAllResultsItem = new ArrayList&lt;AllResultsItem&gt;(); mAllResultsAdapter = new AllResultsAdapter(this, mAllResultsItem); mListView.setAdapter(mAllResultsAdapter); mAllResultsAdapter.notifyDataSetChanged(); } @Override public void bindResources() { mLoadItemsIndicator = (ProgressBar)findViewById(R.id.all_results_view_more_items_indicator); mListView = (ListView)findViewById(R.id.all_results_listview); mViewFlipper = (ViewFlipper)findViewById(R.id.all_results_view_viewflipper); mTitleText = (TextView) findViewById(R.id.all_results_view_title_text); mSettings = getSharedPreferences(WBAConstants.TRACEBUZZ, MODE_PRIVATE); mEdit = mSettings.edit(); } @Override public void bindListeners() { mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; arg0, View view, int id, long arg3) { String url = (String) view.getTag(R.string.item_url); Intent intnt = new Intent(AllResultsActivity.this, WebviewWithControllsActivity.class); if(url.contains("twitter")) { int UQID = (Integer)view.getTag(R.id.twitter_result_identifier); String table = (String)view.getTag(R.string.twitter_table); intnt.putExtra("UQID", UQID); intnt.putExtra("apiKey", apiKey); intnt.putExtra("table", table); } View bar = (View)findViewById(R.id.all_results_title_bar); int barHeight = bar.getHeight(); intnt.putExtra("barHeight", barHeight); intnt.putExtra("URL", url); AllResultsActivity.this.startActivity(intnt); } }); mListView.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { Log.d("","totalItemCount = " + totalItemCount); if (loading) { if (totalItemCount &gt; previousTotal) { loading = false; previousTotal = totalItemCount; itemHolder = previousTotal; } } if (!loading &amp;&amp; (totalItemCount - visibleItemCount) &lt;= (firstVisibleItem + visibleThreshold)) { mLoadItemsIndicator.setVisibility(View.VISIBLE); mListView.invalidate(); mPage++; loadItems(apiKey); loading = true; } else { } } }); } private void loadItems(String passedApiKey) { procesReuest = new ProcesReuest(); procesReuest.execute(identifier, feedIdentifier, passedApiKey); } public void getExtras() { Bundle extras = getIntent().getExtras(); feedIdentifier = null; tagID = null; apiKey = null; if(extras != null) { identifier = extras.getString("identifier"); feedIdentifier = extras.getString("feedidentifier"); apiKey = extras.getString("apiKey"); titleName = extras.getString("tag_name"); tagID = extras.getString("tag_id"); if((titleName != null) &amp;&amp; (!titleName.equals("null"))) { mTitleText.setText(titleName); } } } public void displayDialog() { AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle("Error"); dialog.setMessage("Failed to contact server, check your Internet connection or try again later"); dialog.setNegativeButton("back", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { AllResultsActivity.this.finish(); } }); dialog.show(); } private synchronized void addSyncedItems() throws InterruptedException { if(firstRun){ mAllResultsAdapter.setItems(mAllResultsItem); firstRun = false; } mAllResultsAdapter.notifyDataSetChanged(); mListView.invalidate(); mLoadItemsIndicator.setVisibility(View.GONE); } /** Subclass for processing the ResultReqesut for the specified type **/ private class ProcesReuest extends AsyncTask&lt;String, Void, Exception&gt;{ Downloader downloader = new Downloader(); JSONObject result = null; List&lt;NameValuePair&gt; postParams; boolean timeOut; @Override protected Exception doInBackground(String... params) { Log.d("","doInBackground " + apiKey); try { postParams = new ArrayList&lt;NameValuePair&gt;(2); postParams.add(new BasicNameValuePair("api_key",apiKey)); postParams.add(new BasicNameValuePair("num","100")); postParams.add(new BasicNameValuePair("feed_id", feedIdentifier)); postParams.add(new BasicNameValuePair("page", Integer.toString(mPage))); postParams.add(new BasicNameValuePair("order", "time DESC")); postParams.add(new BasicNameValuePair("tag_id", tagID)); postParams.add(new BasicNameValuePair("format", "json")); Log.d("","postParams = " + postParams); downloadJSON(postParams); proccesJSON(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void onPostExecute(Exception exception) { Log.d("","onPostExecute"); try { addSyncedItems(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(!result.getJSONObject("results").getString("result_count").equals("0")){ mViewFlipper.setDisplayedChild(2); } else { Log.d("","View 3"); mViewFlipper.setDisplayedChild(2); } } catch (Exception e) { // TODO: handle exception } } private void proccesJSON() throws JSONException, IOException { String UIDHolderString = mSettings.getString(WBAConstants.UIDHOLDER, "empty"); JSONArray arrayHolder = null; if(UIDHolderString.equals("empty")){ arrayHolder = new JSONArray(); } else { arrayHolder = new JSONArray(UIDHolderString); } if(result != null){ JSONObject feedResults = result.getJSONObject("results").getJSONObject("result"); Log.d("","" + feedResults); String uniqueItemID = null; Iterator&lt;String&gt; feedResultIter = feedResults.keys(); while (feedResultIter.hasNext()) { uniqueItemID = (String) feedResultIter.next(); Log.d("","" + uniqueItemID); if(!uniqueItemID.equals("metaresult")) { AllResultsItem item = new AllResultsItem(); if(UIDHolderString.contains(uniqueItemID)) { item.setNewFeed(0); } else { item.setNewFeed(1); arrayHolder.put(uniqueItemID); } JSONObject resultItem = feedResults.getJSONObject(uniqueItemID); String typeFeed = resultItem.getString("type"); if(!typeFeed.contains("twitter") &amp;&amp; typeFeed != null){ if(resultItem.has("tags") &amp;&amp; resultItem.getString("tags")!= null) { JSONObject tags = resultItem.getJSONObject("tags"); Iterator&lt;String&gt; tagIter = tags.keys(); ArrayList&lt;String&gt; tagHolder = new ArrayList&lt;String&gt;(); while (tagIter.hasNext()) { String tagID = (String) tagIter.next(); String tagName = tags.getString(tagID); tagHolder.add(tagName); } item.setTagNames(tagHolder.toString()); } item.setType(typeFeed); if((resultItem.has("title")) &amp;&amp; (!resultItem.isNull("title")) &amp;&amp; (!resultItem.getString("title").equals("null") )) { item.setResultTitle(resultItem.getString("title")); } if(resultItem.has("unique_id")) { item.setResultIdentifier(Integer.parseInt(resultItem.getString("unique_id"))); } if(resultItem.has("src_link") &amp;&amp; (resultItem.getString("src_link") != null)) { item.setMessageURL(resultItem.getString("src_link")); } if(resultItem.has("date_time")) { item.setDate( resultItem.getString("date_time")); } if(resultItem.has("description")) { item.setMessage(resultItem.getString("description")); } } else if(resultItem.getString("type").contains("twitter")){ Icon icon = new Icon(); if((resultItem.has("title")) &amp;&amp; (!resultItem.isNull("title")) &amp;&amp; (!resultItem.getString("title").equals("null"))) { item.setUserTwitter(resultItem.getString("title")); } if(resultItem.has("unique_id")) { item.setIdentifier(Integer.parseInt(resultItem.getString("unique_id"))); } if(resultItem.has("src_link") &amp;&amp; (resultItem.getString("src_link") != null)) { item.setMessageURL(resultItem.getString("src_link")); } if(resultItem.has("date_time")) { item.setDate(resultItem.getString("date_time")); } if(resultItem.has("description")) { item.setMessageText(resultItem.getString("description")); } if(resultItem.has("image_link")) { if(!resultItem.has("null")){ icon.setImageURL(resultItem.getString("image_link")); } } if(resultItem.has("table")) { item.setTable(resultItem.getString("table")); } item.setType(typeFeed); item.addIconsObject(icon); if(resultItem.has("tags") &amp;&amp; resultItem.getString("tags")!= null) { JSONObject tags = resultItem.getJSONObject("tags"); Iterator&lt;String&gt; tagIter = tags.keys(); ArrayList&lt;String&gt; tagHolder = new ArrayList&lt;String&gt;(); while (tagIter.hasNext()) { String tagID = (String) tagIter.next(); String tagName = tags.getString(tagID); tagHolder.add(tagName); } item.setTagNames(tagHolder.toString()); } } mAllResultsItem.add(item); } } mEdit.putString(WBAConstants.UIDHOLDER, arrayHolder.toString()); mEdit.commit(); } } public boolean downloadJSON(List&lt;NameValuePair&gt;postParams) { try { result = downloader.getJSONFromURL("http://www.example.com/webservice/get_feed_results.php", true, postParams); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } } } </code></pre> <p>The reason for the Syncronized method was just for trying to get the adapter and the listview the update the same time.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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