title: Adding a button to a card with the RecyclerView pattern
body: <p>In accordance to <a href="https://developer.android.com/training/material/lists-cards.html" rel="nofollow noreferrer">Creating Lists and Cards</a>, and the RecyclerView pattern, I have implemented an activity that shows cards for an array of items. I'd like each Card to have a button that pops a Dialog of options for that item:</p>
  
  <p><img src="https://i.stack.imgur.com/co9KN.png" alt="enter image description here"></p>
  
  <p>as you can see there's a button to the upper right corner of each card. I have implemented this like so:</p>
  
  <p><strong>CardViewHolder.java:</strong></p>
  
  <pre><code>public class CardViewHolder extends RecyclerView.ViewHolder {
      protected TextView itemTitle;
      protected TextView itemCode;
      protected TextView itemDate;
      protected TextView itemStatus;
      protected Button optionsButton;
      public CardViewHolder(View v) {
          super(v);
          itemTitle = (TextView) v.findViewById(R.id.itemTitle);
          itemCode = (TextView) v.findViewById(R.id.itemCode);
          itemStatus = (TextView) v.findViewById(R.id.itemStatus);
          itemDate = (TextView) v.findViewById(R.id.itemDate);
          optionsButton = (Button) v.findViewById(R.id.options_button);
      }
  }
  </code></pre>
  
  <p>And then, Implemented my DataAdapter which is set in my activity for my RecyclerView:</p>
  
  <p><strong>DataAdapter.java:</strong></p>
  
  <pre><code>public class DataAdapter extends RecyclerView.Adapter&lt;CardViewHolder&gt;{
      private JSONArray mDataSet;
      private Context mContext;
  
      //constructor
      public DataAdapter(JSONArray myDataSet, Context c) {
          mContext = c;
          mDataSet = myDataSet;
      }
      //creates new views (invoked by the layout manager)
      @Override
      public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          //create a new view
          View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_text_view, parent, false);
          //set the view's size, margins, padding and layout parameters
          CardViewHolder vh = new CardViewHolder(v);
          return vh;
      }
  
      @Override
      public void onBindViewHolder(CardViewHolder holder, int position) {
          //get element from dataset at position
          //replace the contets of the view with that element
          String title,itemCode,itemStatus, itemDate;
          title = itemCode = itemDate = itemStatus = "";
          try {
              JSONObject item = new JSONObject(mDataSet.get(position).toString());
              title = item.getString("desc");
              itemCode = item.getString("code");
              itemDate = item.getString("date");
              itemStatus = item.getString("status");
  
          } catch (JSONException e) {
              e.printStackTrace();
          }
          holder.itemTitle.setText(title);
          holder.itemCode.setText(itemCode);
          holder.itemDate.setText(itemDate);
          holder.itemStatus.setText(Html.fromHtml(itemStatus));
          holder.optionsButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Toast.makeText(mContext,title,Toast.LENGTH_LONG).show();
              }
          });
      }
  
      @Override
      public int getItemCount() {
          return mDataSet.length();
      }
  
      // Provide a reference to the views for each data item
      // Complex data items may need more than one view per item, and
      // you provide access to all the views for a data item in a view holder
      public static class ViewHolder extends RecyclerView.ViewHolder {
          // each data item is just a string in this case
          public TextView mTextView;
          public ViewHolder(TextView v) {
              super(v);
              mTextView = v;
          }
      }
  }
  </code></pre>
  
  <p>My questions and thoughts about this are:<br></p>
  
  <ul>
  <li><p>how do i pass this button onClick listener the values of this item? trying to access <code>title</code> for example from within the onClick handler is an error because it's not a final variable and it's accessed from within an inner class. On the other hand, If i make my DataAdapter class implement View.onClickListener interface, I'm still unsure how do I get those item specific values from within onClick method.<br></p></li>
  <li><p>Is this considered a good practice at all? (Passing the application context to my DataAdapter)? Should i be attacking this from another angle maybe?</p></li>
  </ul>