[Android] Fix media delete algo and run it in background

Geoffrey Métais git at videolan.org
Mon Dec 12 16:15:09 CET 2016


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Dec 12 15:38:00 2016 +0100| [449ed3c3f719719ed0b2dfe82a7287f77d36b91a] | committer: Geoffrey Métais

Fix media delete algo and run it in background

> https://code.videolan.org/videolan/vlc-android/commit/449ed3c3f719719ed0b2dfe82a7287f77d36b91a
---

 .../vlc/gui/audio/AudioBrowserAdapter.java         | 45 ++++++++++------------
 1 file changed, 21 insertions(+), 24 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
index bb88f80..1e7bb49 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
@@ -199,38 +199,35 @@ public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapte
     }
 
     public void remove(final int position) {
-        MediaLibraryItem[] dataList = new MediaLibraryItem[getItemCount()-1];
-        int offset = 0;
-        for (int i = 0; i < getItemCount(); ++i) {
-            if (i == position)
-                ++offset;
-            else
-                dataList[i] = mDataList[i+offset];
-        }
-        mDataList = dataList;
-        mContext.runOnUiThread(new Runnable() {
+        VLCApplication.runBackground(new Runnable() {
             @Override
             public void run() {
-                notifyItemRemoved(position);
+                final MediaLibraryItem[] dataList = new MediaLibraryItem[getItemCount()-1];
+                Util.removePositionInArray(mDataList, position, dataList);
+                mContext.runOnUiThread(new Runnable() {
+                    @Override
+                    public void run() {
+                        mDataList = dataList;
+                        notifyItemRemoved(position);
+                    }
+                });
             }
         });
     }
 
-    public void addItem(final int position, MediaLibraryItem item) {
-        MediaLibraryItem[] dataList = new MediaLibraryItem[getItemCount()+1];
-        int offset = 0;
-        for (int i = 0; i < getItemCount(); ++i) {
-            if (i == position) {
-                dataList[position] = item;
-                ++offset;
-            } else
-                dataList[i] = mDataList[i-offset];
-        }
-        mDataList = dataList;
-        mContext.runOnUiThread(new Runnable() {
+    public void addItem(final int position, final MediaLibraryItem item) {
+        VLCApplication.runBackground(new Runnable() {
             @Override
             public void run() {
-                notifyItemInserted(position);
+                final MediaLibraryItem[] dataList = new MediaLibraryItem[getItemCount()+1];
+                Util.addItemInArray(mDataList, position, item, dataList);
+                mContext.runOnUiThread(new Runnable() {
+                    @Override
+                    public void run() {
+                        mDataList = dataList;
+                        notifyItemInserted(position);
+                    }
+                });
             }
         });
     }



More information about the Android mailing list