[Android] Better adapter notifying in case of delete

Geoffrey Métais git at videolan.org
Fri Nov 20 18:18:59 CET 2015


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Nov 20 18:18:16 2015 +0100| [ac0c82947191475a389e059346a40f1b3740c732] | committer: Geoffrey Métais

Better adapter notifying in case of delete

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

 .../videolan/vlc/gui/video/VideoGridFragment.java  |    2 ++
 .../videolan/vlc/gui/video/VideoListAdapter.java   |   24 +++++++++++++++++---
 2 files changed, 23 insertions(+), 3 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 a0405a9..311ecaf 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
@@ -489,6 +489,8 @@ public class VideoGridFragment extends MediaBrowserFragment implements ISortable
 
     public void deleteMedia(int position){
         final MediaWrapper media = mVideoAdapter.getItem(position);
+        if (media == null)
+            return;
         final String path = media.getUri().getPath();
         FileUtils.asyncRecursiveDelete(path);
         mMediaLibrary.getMediaItems().remove(media);
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
index 4a80281..5bc5063 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -26,6 +26,7 @@ import android.databinding.ViewDataBinding;
 import android.graphics.Bitmap;
 import android.graphics.drawable.BitmapDrawable;
 import android.support.annotation.MainThread;
+import android.support.annotation.Nullable;
 import android.support.v4.util.ArrayMap;
 import android.support.v7.widget.RecyclerView;
 import android.view.LayoutInflater;
@@ -200,8 +201,12 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
         return mSortDirection * compare;
     }
 
+    @Nullable
     public MediaWrapper getItem(int position) {
-        return mVideos.get(position);
+        if (position < 0 || position > mVideos.size())
+            return null;
+        else
+            return mVideos.get(position);
     }
 
     public void add(MediaWrapper item) {
@@ -210,8 +215,21 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
 
     @MainThread
     public void remove(MediaWrapper item) {
-        mVideos.remove(item);
-        notifyDataSetChanged();
+        int position = getItemPosition(item);
+        if (position == -1)
+            return;
+        mVideos.remove(position);
+        notifyItemRemoved(position);
+    }
+
+    private int getItemPosition(MediaWrapper mw) {
+        if (mw == null || mVideos.isEmpty())
+            return -1;
+        for (int i = 0 ; i < mVideos.size(); ++i){
+            if (mw.equals(mVideos.get(i)))
+                return i;
+        }
+        return -1;
     }
 
     public void addAll(Collection<MediaWrapper> items) {



More information about the Android mailing list