title: How is the position of a RecyclerView adapter related to the index of its dataset?
body: <p>I thought they were the same, but they're not.  The following code gives an indexOutOfBounds exception when I try to access the "position" index of my dataset, in this case a list of a model I created called Task:</p>
  
  <pre><code>public class TaskAdapter extends RecyclerView.Adapter&lt;TaskAdapter.TaskViewHolder&gt;   {
  
  private List&lt;Task&gt; taskList;
  private TaskAdapter thisAdapter = this;
  
  // cache of views to reduce number of findViewById calls
  public static class TaskViewHolder extends RecyclerView.ViewHolder {
      protected TextView taskTV;
      protected ImageView closeBtn;
  
      public TaskViewHolder(View v) {
          super(v);
          taskTV = (TextView)v.findViewById(R.id.taskDesc);
          closeBtn = (ImageView)v.findViewById(R.id.xImg);
      }
  }
  
  
  public TaskAdapter(List&lt;Task&gt; tasks) {
      if(tasks == null)
          throw new IllegalArgumentException("tasks cannot be null");
      taskList = tasks;
  }
  
  
  // onBindViewHolder binds a model to a viewholder
  @Override
  public void onBindViewHolder(TaskViewHolder taskViewHolder, int pos) {
      final int position = pos;
      Task currTask = taskList.get(pos);
      taskViewHolder.taskTV.setText(currTask.getDescription());
  
      **taskViewHolder.closeBtn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Log.d("TRACE", "Closing task at position " + position);
              // delete from SQLite DB
              Task taskToDel = taskList.get(position);
              taskToDel.delete();
              // updating UI
              taskList.remove(position);
              thisAdapter.notifyItemRemoved(position);
          }
      });**
  }
  
  @Override
  public int getItemCount() {
      //Log.d("TRACE", taskList.size() + " tasks in DB");
      return taskList.size();
  }
  
  
  // inflates row to create a viewHolder
  @Override
  public TaskViewHolder onCreateViewHolder(ViewGroup parent, int pos) {
      View itemView = LayoutInflater.from(parent.getContext()).
                                     inflate(R.layout.list_item, parent, false);
      Task currTask = taskList.get(pos);
  
      //itemView.setBackgroundColor(Color.parseColor(currTask.getColor()));
      return new TaskViewHolder(itemView);
  }
  }
  </code></pre>
  
  <p>Deleting from my recyclerview gives unexpected results sometimes.  Sometimes the element ahead of the one clicked is deleted, other times an indexOutOfBounds exception occurs at "taskList.get(position)".  </p>
  
  <p>Reading <a href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html">https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html</a> and <a href="https://developer.android.com/training/material/lists-cards.html">https://developer.android.com/training/material/lists-cards.html</a> did not give me any more insight into why this was happening and how to fix it.  </p>
  
  <p>It looks like RecyclerView recycles the rows, but I wouldn't expect an indexoutofbounds exception using a smaller subset of numbers to index my list.</p>