自定义适配器更改时滚动的简单修复?

时间:2014-04-15 10:34:27

标签: android android-listview arraylist android-asynctask

使用我的自定义适配器 - 我使用listiew填充AsyncTaskdoInBackground更新用于自定义适配器的ArrayLists。 onProgressUpdate调用adapter.notifyDataSetChanged();

当加载大量文件时,我希望UI能够响应,但是当你在列表仍然被填充时尝试滚动时,我收到了这个错误:

 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. Make sure your adapter calls notifyDataSetChanged() when its content changes.


@Override
        protected Boolean doInBackground(DbxFileSystem... params) {
            //Opens thumbnails for each image contained in the dropbox folder
            try {
                DbxFileSystem fileSystem = params[0];
                numFiles = fileSystem.listFolder(currentPath).size();
                for (DbxFileInfo fileInfo: fileSystem.listFolder(currentPath)) {
                    String filename = fileInfo.path.getName();


                    try{
                        if(!fileInfo.isFolder)
                        {
                            Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                            pix.add(image);
                            paths.add(fileInfo.path);
                            publishProgress(1); //use this to update the ListView
                        }
                        else
                        {
                            //must be a folder if it has no thumb, so add folder icon
                            Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.dbfolder);
                            pix.add(image);
                            paths.add(fileInfo.path);
                            publishProgress(1);


                        }

                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                    System.gc();
                }

            }
            catch (Exception e) {
                e.printStackTrace();
                return false;
            } finally {
            }
            return true;
        }

       @Override
        protected void onProgressUpdate(Integer...progress) {

           if(pix.size()==1) // //not ideal but works for now, only bind the adapter if its the first time we have looped through.
           {
           adapter = new ImageAdapter(getApplicationContext(), pix, paths, numFiles);
           lstView.setAdapter(adapter);
           }

           adapter.notifyDataSetChanged();
           lstView.requestLayout();
           super.onProgressUpdate(progress);
        }

任何人都可以看到问题在这里吗?我该怎么做才能防止它呢? 我最初使用的是一个进度条,只有在加载后才显示填充内容,但我更愿意显示增量加载,让用户在加载内容时滚动。

P.S。我认为这是一个很常见的问题,已经阅读了几个类似的问题,但我仍然无法解决我需要改变的问题。

1 个答案:

答案 0 :(得分:0)

你的适配器有pix,numFiles和path作为数据源,因为你在 doInBackground()中修改那些在非UI线程上运行 的集合你会得到这个异常。

new ImageAdapter(getApplicationContext(), pix, paths, numFiles);通过参考传递这些集合。