[Android] Fix position management for MRL click events

Geoffrey Métais git at videolan.org
Wed Jun 22 18:07:04 CEST 2016


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Jun 22 16:10:36 2016 +0200| [1a586537ede32b49e7be54a33f574db4950e356b] | committer: Geoffrey Métais

Fix position management for MRL click events

> https://code.videolan.org/videolan/vlc-android/commit/1a586537ede32b49e7be54a33f574db4950e356b
---

 .../org/videolan/vlc/gui/network/MRLAdapter.java   | 29 ++++++++++++++--------
 1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/network/MRLAdapter.java b/vlc-android/src/org/videolan/vlc/gui/network/MRLAdapter.java
index e1522a2..761764d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/network/MRLAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/network/MRLAdapter.java
@@ -60,24 +60,26 @@ public class MRLAdapter extends RecyclerView.Adapter<MRLAdapter.ViewHolder> {
     }
 
     @Override
-    public void onBindViewHolder(final ViewHolder holder, final int position) {
-        final String uri = mDataset.get(position);
-        holder.uriTv.setText(uri);
+    public void onBindViewHolder(final ViewHolder holder, int position) {
+        holder.uriTv.setText(mDataset.get(position));
         holder.uriTv.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 UiTools.setKeyboardVisibility(holder.itemView, false);
-                MediaUtils.openStream(v.getContext(), uri);
+                MediaUtils.openStream(v.getContext(), getItem(holder.getAdapterPosition()));
             }
         });
         holder.deleteButton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 UiTools.setKeyboardVisibility(holder.itemView, false);
-                if (holder.getAdapterPosition() > -1) {
-                    final String mrl = mDataset.get(position);
-                    mDataset.remove(position);
-                    notifyItemRemoved(position);
+                final int currentPosition = holder.getAdapterPosition();
+                if (currentPosition > -1) {
+                    final String mrl = getItem(currentPosition);
+                    if (mrl == null)
+                        return;
+                    mDataset.remove(currentPosition);
+                    notifyItemRemoved(currentPosition);
                     UiTools.snackerWithCancel(holder.itemView,
                             holder.itemView.getContext().getString(R.string.file_deleted),
                             new Runnable() {
@@ -88,8 +90,8 @@ public class MRLAdapter extends RecyclerView.Adapter<MRLAdapter.ViewHolder> {
                             }, new Runnable() {
                                 @Override
                                 public void run() {
-                                    mDataset.add(position, mrl);
-                                    notifyItemInserted(position);
+                                    mDataset.add(currentPosition, mrl);
+                                    notifyItemInserted(currentPosition);
                                 }
                             });
                 }
@@ -101,6 +103,13 @@ public class MRLAdapter extends RecyclerView.Adapter<MRLAdapter.ViewHolder> {
         mDataset = list;
         notifyDataSetChanged();
     }
+
+    public String getItem(int position) {
+        if (position >= getItemCount() || position < 0)
+            return null;
+        return mDataset.get(position);
+    }
+
     @Override
     public int getItemCount() {
         return mDataset.size();



More information about the Android mailing list