1

First of all thanks to CommonsWare for this great component. But I have an issue which has been giving me grief some times. That is --> 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.

The above is the error I get sometimes, but sometimes not. The below is my code.

 protected boolean cacheInBackground() {

                  JSONObject json = null;
                  if(results != null)
                     results.clear();

                  results = new ArrayList<DataSource>();// results is the global variable which is List<DataSource> results; and initialized it here.

                  List<NameValuePair> params = new ArrayList<NameValuePair>();

                   params.add(new BasicNameValuePair("postdata",passeddata));
                   params.add(new BasicNameValuePair("currentpage", (++index)+""));

            json = jParser.makeHttpRequest(urlphp, "POST", params);   // Here is the line that makes http request for paginated results based on index and gets the response.
                  try {
                        int success = json.getInt("success");

                        if (success == 1) {

                            rowcount = json.getInt("numrows");

                            Log.d("row count value is>>>>",""+rowcount); // Here rowcount is the total rows in the database

                            products = json.getJSONArray("products");

                            // looping through All Products
                            for (int i = 0; i < products.length(); i++) {
                                JSONObject c = products.getJSONObject(i);

                              // Storing each json item in variable
                            String id = c.getString(ID);
                            String price = c.getString(PRICE);
                            String name = c.getString(NAME);

                DataSource data = new DataSource(id,name,price);

                            results.add(data);


                        }
                    }else{
                          // product not found

                        successpass = success;
                        message = json.getString("message");

                          return false;

                      }
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }

                  return(getWrappedAdapter().getCount()<rowcount);


          }


     @Override
      protected void appendCachedData() {
        if (getWrappedAdapter().getCount()<rowcount) {
          @SuppressWarnings("unchecked")
          CustomListAdapter adapter = (CustomListAdapter)getWrappedAdapter();

          //ArrayAdapter<Integer> a=(ArrayAdapter<Integer>)getWrappedAdapter();

            for (DataSource d : results){
                adapter.add(d);
            }

        }else{

        }
      }

As you can see in the code I am storing data into Bean class object and then storing that each object in List. I have implemented CustomListView to show the details in a list.

However sometimes the above error arises and sometimes not. Have searched a lot for this and found this and some other general adapter issues like this etc., But I get this error even without clicking on placeholder and even without calling notifyDataSetChanged(). As you can see in my code I never called notifyDataSetChanged(), but why this error occurs? Is there something wrong with my implementation coding? Can some one please help me solve this issue? As I deeply integrated this component in my application, I am scared now to see for alternatives.

Below is the stack trace:

03-13 16:12:07.662: E/AndroidRuntime(494): FATAL EXCEPTION: main
03-13 16:12:07.662: E/AndroidRuntime(494): 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(2130968671, class android.widget.ListView) with Adapter(class com.example.onlinedata.ORAdsList$CustomisedEndlessAdapter)]
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.ListView.layoutChildren(ListView.java:1492)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.AbsListView.onLayout(AbsListView.java:1147)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.view.View.layout(View.java:7035)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.view.View.layout(View.java:7035)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.view.View.layout(View.java:7035)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.view.View.layout(View.java:7035)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.view.View.layout(View.java:7035)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.view.View.layout(View.java:7035)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.os.Looper.loop(Looper.java:123)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-13 16:12:07.662: E/AndroidRuntime(494):  at java.lang.reflect.Method.invokeNative(Native Method)
03-13 16:12:07.662: E/AndroidRuntime(494):  at java.lang.reflect.Method.invoke(Method.java:521)
03-13 16:12:07.662: E/AndroidRuntime(494):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-13 16:12:07.662: E/AndroidRuntime(494):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-13 16:12:07.662: E/AndroidRuntime(494):  at dalvik.system.NativeStart.main(Native Method)
Community
  • 1
  • 1
rick
  • 4,665
  • 10
  • 27
  • 44
  • Please post the entire stack trace. – CommonsWare Mar 13 '13 at 10:33
  • @CommonsWare I never thought I could thank you directly. Thank you very much. I updated my question with stack trace. I think only you can solve my problem. – rick Mar 13 '13 at 10:47
  • The only way that error occurs is if you modify the contents of the adapter on a background thread. `EndlessAdapter` should not be doing that, which means either you are doing it yourself someplace else, or there is a bug in `EndlessAdapter`. In order to have a hope of fixing such a bug, I need a repeatable test case. If you happen to create a repeatable test case, attach it (along with the steps I need to follow to use it) on [the issue that you opened](https://github.com/commonsguy/cwac-endless/issues/28). – CommonsWare Mar 13 '13 at 10:51
  • @CommonsWare But I pasted my detailed code above. As it is shown, the data is addded to the adapter only in appendCachedData() by this line adapter.add(d); :( I still don't know what causes that error. How to make a reproducible test case? Because it happens in my app for few times. – rick Mar 13 '13 at 10:59
  • "How to make a reproducible test case?" -- if I knew that, I would have done it myself. I have never encountered this error. Hence, I have no way to fix it, because even if I randomly make changes to the code, I have no idea if those random changes actually help. – CommonsWare Mar 13 '13 at 11:03
  • The thing is sometimes it happens and sometimes it doesn't even when I follow the same steps in my application itself. Coming to a reproducible test case--> I was just asking whether I need to make a demo kinda thing to reproduce that case? – rick Mar 13 '13 at 11:12
  • "I was just asking whether I need to make a demo kinda thing to reproduce that case?" -- only if you can come up with a series of steps that causes the error to always occur. Otherwise, it will not help either of us. I will poke at the code sometime to see if I can find an issue purely by eyeball, but I am not hopeful. – CommonsWare Mar 13 '13 at 11:19
  • Yeah. I understand. This doesn't occur all the times. It's very strange that it happens only few times though I follow the same steps when it caused the error. – rick Mar 13 '13 at 12:52
  • We seem to be having this issue when updating the values in the endlessadapter (and calling notifyDataSetChanged) between a TouchEvent DOWN and the TouchEvent UP. So TouchEvent Down -> update values -> TouchEvent UP -> this illegalstateexception – vbsteven Mar 27 '13 at 10:12

1 Answers1

3

Somebody finally gave me a reproducible test case, and this bug is now fixed as of v1.2.1. Grab a fresh copy of the repo, or a fresh JAR. No code changes should be required over v1.2.0.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Is this what caused crash even when I tested. But I guess I got this randomly. Anyways, gonna include new jar and test it. Thanks a lot for posting back. – rick Apr 29 '13 at 03:51