开发者

java.lang.IllegalStateException? How to get rid of this?

I am using a list view. In which i change the content or add contents when scroll. It works fine but if i navigate 2 or three times from that page, it gives an error. The Error 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. [in ListView(2131230910, class android.widget.ListView) with Adapter.


    @Override
    public View getView(final int position, View convertView, ViewGroup arg2)
    {
        Log.e("vec pos", ""+position);
        if(position == vectProductBySearch.size()-1 && scrollState == 0 && vectProductBySearch.size()>8)
        {
            Log.e("vec pos", "in if()");
            if (progDialog != null && progDialog.isShowing())
                progDialog.dismiss();
            progDialog = ProgressDialog.show(ProducyBySearch.this, "",
                    "Loading... Please wait..", true);

            new Thread(new Runnable() 
            {
                @Override
                public void run() 
                {
                    SearchByCatagory.startIndex= SearchByCatagory.startIndex+10;
                    post.getProductBySearch(AppsConstants.email, AppsConstants.password, AppsConstants.AuthenticationToken,  SearchByCatagory.CatagoryId,  SearchByCatagory.description,  SearchByCatagory.brand, SearchByCatagory.rating, AppsConstants.UserNum, SearchByCatagory.startIndex);
                    runOnUiThread(new Runnable() 
                    {
                        public void run() 
                        {
                            deafaultAdapter.refresh(AppsConstants.vectProductBySearch);
                            if (progDialog != null && progDialog.isShowing())
                  开发者_开发技巧              progDialog.dismiss();
                        }
                    });

                }
            }).start();


Use a handler to force any updates from background threads to occur within the UI thread. UI's in nearly all environments/languages don't support changes from other threads


Make sure you are using the UiThread to modify the content of the list and when the list content changes do not forget to call youradapter.notifyDataSetChanged().


outside from the ui-thread you can do ui-operations using post. just create a new inline runnable-object and add your code to the run()-method. its not that beatiful but helps in most cases.

example:

view.post(new Runnable() {

        @Override
        public void run() {
            view.scrollTo(0, 0));

        }
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜