[Android] VideoGrid: fix CalledFromWrongThreadException

Thomas Guillem git at videolan.org
Mon Oct 12 15:46:20 CEST 2015


vlc-ports/android | branch: master | Thomas Guillem <thomas at gllm.fr> | Mon Oct 12 14:13:05 2015 +0200| [5cfd94e371a6da31885b0f23f6618f0d4581b6a3] | committer: Jean-Baptiste Kempf

VideoGrid: fix CalledFromWrongThreadException

For some reasons, setNotifyOnChanged(false) doesn't work on older devices
(2.3), as a result, notifyDataSetChanged is called from a background thread.

Signed-off-by: Jean-Baptiste Kempf <jb at videolan.org>

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=5cfd94e371a6da31885b0f23f6618f0d4581b6a3
---

 .../videolan/vlc/gui/video/VideoGridFragment.java   |   19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
index 752fa3d..91e4bdf 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
@@ -76,6 +76,7 @@ import org.videolan.vlc.util.WeakHandler;
 import org.videolan.vlc.widget.SwipeRefreshLayout;
 
 import java.io.File;
+import java.util.ArrayList;
 import java.util.List;
 
 public class VideoGridFragment extends MediaBrowserFragment implements ISortable, IVideoBrowser, SwipeRefreshLayout.OnRefreshListener, AdapterView.OnItemClickListener {
@@ -416,13 +417,12 @@ public class VideoGridFragment extends MediaBrowserFragment implements ISortable
             VLCApplication.runBackground(new Runnable() {
                 @Override
                 public void run() {
-                    mVideoAdapter.setNotifyOnChange(false);
-                    mVideoAdapter.clear();
+                    final ArrayList<MediaWrapper> displayList = new ArrayList<MediaWrapper>();
 
                     if (mGroup != null || itemList.size() <= 10) {
                         for (MediaWrapper item : itemList) {
                             if (mGroup == null || item.getTitle().startsWith(mGroup)) {
-                                mVideoAdapter.add(item);
+                                displayList.add(item);
                                 if (mThumbnailer != null)
                                     mThumbnailer.addJob(item);
                             }
@@ -431,12 +431,21 @@ public class VideoGridFragment extends MediaBrowserFragment implements ISortable
                     else {
                         List<MediaGroup> groups = MediaGroup.group(itemList);
                         for (MediaGroup item : groups) {
-                            mVideoAdapter.add(item.getMedia());
+                            displayList.add(item.getMedia());
                             if (mThumbnailer != null)
                                 mThumbnailer.addJob(item.getMedia());
                         }
                     }
-                    mVideoAdapter.setNotifyOnChange(true);
+
+                    if (getActivity() != null) {
+                        getActivity().runOnUiThread(new Runnable() {
+                            @Override
+                            public void run() {
+                                mVideoAdapter.clear();
+                                mVideoAdapter.addAll(displayList);
+                            }
+                        });
+                    }
                     if (mReadyToDisplay)
                         display();
                 }



More information about the Android mailing list