title: How to access the data source of a RecyclerView adapter's ViewHolder?
body: <p>The constructor of my RecyclerView's adapter looks like:</p>
  
  <pre><code>Context context;
  List&lt;ConnectionItem&gt; connections;
  
  public ConnectionsListAdapter(Context context, List connections) {
      this.context = context;
      this.connections = connections;
  }
  </code></pre>
  
  <p>After the adapter's declarations, I declare a static ViewHolder class for the RecyclerView, which also handles the onClicks of any buttons:</p>
  
  <pre><code>public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
  
      public Context context;
  
      public ImageView connectionImage;
      public TextView connectionName;
      public ImageButton startMsg;
  
      public ViewHolder(View itemView, List&lt;ConnectionItem&gt; connections) {
          super(itemView);
          ...
          startMsg.setOnClickListener(this);
      }
  
      @Override
      public void onClick(View v) {
          Intent intent = new Intent(v.getContext(), ChatActivity.class);
          intent.putExtra("name", connections.get(getAdapterPosition()).getName()); // Error because accessing from static context
          intent.putExtra("id", connections.get(getAdapterPosition()).getUid()); // Error because accessing from static context
          context.startActivity(intent);
      }
  }
  </code></pre>
  
  <p>The problem is that <code>connections</code> is not accessible from a static context in the ViewHolder static class. What's the best way for my ViewHolder to get information from the RecyclerView adapter? As a workaround, I am passing the data source to the constructor of the ViewHolder and having a new instance variable in the ViewHolder for the data source:</p>
  
  <pre><code>public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
      public Context context;
      public List&lt;ConnectionItem&gt; connections;
  
      public ImageView connectionImage;
      public TextView connectionName;
      public ImageButton startMsg;
  
  public ViewHolder(View itemView, List&lt;ConnectionItem&gt; connections) {
          super(itemView);
          this.connections = connections;
          ...
          startMsg.setOnClickListener(this);
  }
  
      @Override
      public void onClick(View v) {
          Intent intent = new Intent(v.getContext(), ChatActivity.class);
          intent.putExtra("name", connections.get(getAdapterPosition()).getName()); // Okay now
          intent.putExtra("id", connections.get(getAdapterPosition()).getUid()); // Okay now
          context.startActivity(intent);
      }
  </code></pre>
  
  <p>Does my way at all break the ViewHolder pattern or create any other problems in the future?</p>