Commit 7fb80518 authored by Geoffrey Métais's avatar Geoffrey Métais

Fix position management for MRL click events

parent c812e8ca
...@@ -60,24 +60,26 @@ public class MRLAdapter extends RecyclerView.Adapter<MRLAdapter.ViewHolder> { ...@@ -60,24 +60,26 @@ public class MRLAdapter extends RecyclerView.Adapter<MRLAdapter.ViewHolder> {
} }
@Override @Override
public void onBindViewHolder(final ViewHolder holder, final int position) { public void onBindViewHolder(final ViewHolder holder, int position) {
final String uri = mDataset.get(position); holder.uriTv.setText(mDataset.get(position));
holder.uriTv.setText(uri);
holder.uriTv.setOnClickListener(new View.OnClickListener() { holder.uriTv.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
UiTools.setKeyboardVisibility(holder.itemView, false); UiTools.setKeyboardVisibility(holder.itemView, false);
MediaUtils.openStream(v.getContext(), uri); MediaUtils.openStream(v.getContext(), getItem(holder.getAdapterPosition()));
} }
}); });
holder.deleteButton.setOnClickListener(new View.OnClickListener() { holder.deleteButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
UiTools.setKeyboardVisibility(holder.itemView, false); UiTools.setKeyboardVisibility(holder.itemView, false);
if (holder.getAdapterPosition() > -1) { final int currentPosition = holder.getAdapterPosition();
final String mrl = mDataset.get(position); if (currentPosition > -1) {
mDataset.remove(position); final String mrl = getItem(currentPosition);
notifyItemRemoved(position); if (mrl == null)
return;
mDataset.remove(currentPosition);
notifyItemRemoved(currentPosition);
UiTools.snackerWithCancel(holder.itemView, UiTools.snackerWithCancel(holder.itemView,
holder.itemView.getContext().getString(R.string.file_deleted), holder.itemView.getContext().getString(R.string.file_deleted),
new Runnable() { new Runnable() {
...@@ -88,8 +90,8 @@ public class MRLAdapter extends RecyclerView.Adapter<MRLAdapter.ViewHolder> { ...@@ -88,8 +90,8 @@ public class MRLAdapter extends RecyclerView.Adapter<MRLAdapter.ViewHolder> {
}, new Runnable() { }, new Runnable() {
@Override @Override
public void run() { public void run() {
mDataset.add(position, mrl); mDataset.add(currentPosition, mrl);
notifyItemInserted(position); notifyItemInserted(currentPosition);
} }
}); });
} }
...@@ -101,6 +103,13 @@ public class MRLAdapter extends RecyclerView.Adapter<MRLAdapter.ViewHolder> { ...@@ -101,6 +103,13 @@ public class MRLAdapter extends RecyclerView.Adapter<MRLAdapter.ViewHolder> {
mDataset = list; mDataset = list;
notifyDataSetChanged(); notifyDataSetChanged();
} }
public String getItem(int position) {
if (position >= getItemCount() || position < 0)
return null;
return mDataset.get(position);
}
@Override @Override
public int getItemCount() { public int getItemCount() {
return mDataset.size(); return mDataset.size();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment