title: Build AlerDialog outside of an Activity (RecyclerView.Viewholder)
body: <p>I'm using a RecyclerView and want to open an AlertDialog whenever an item in the RecyclerView is long-clicked.</p>
  
  <p>In the RecyclerView's ViewHolder, i have implemented the clicklistener</p>
  
  <pre><code> @Override
      public boolean onLongClick(View v) {
  
          //Open Alert dialog to delete item
  
          AlertDialog.Builder alert = new AlertDialog.Builder(context);
  
          ...
  
  }
  </code></pre>
  
  <p>As i can read here: <a href="http://goo.gl/Kd1c7i" rel="nofollow">http://goo.gl/Kd1c7i</a> one needs to pass "MyActivity.this" to the AlertDialog.Builder constructor but since the ViewHolder is a static part of the RecyclerView Adapter, i cannot refer to "MyActivity.this".
  Hence i still get the error:</p>
  
  <pre><code>android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
  </code></pre>
  
  <p>So how can i create an AlertDialog from class which does not extend Activity?</p>
  
  <p>@Edit: More code for more information:</p>
  
  <p>Here is my RecyclerView Adapter constructor:</p>
  
  <pre><code>RecyclerViewCourseAdapter(ArrayList&lt;Course&gt; courses, Context context) {
      this.context = context;
      if (courses == null) {
          throw new IllegalArgumentException("courses ArrayList must not be null");
      }
  
      mCourseArrayList = courses;
  }
  </code></pre>
  
  <p>I store the given context as private in the Adapter class:</p>
  
  <pre><code>private Context context;
  </code></pre>
  
  <p>This is my ViewHolder:</p>
  
  <pre><code>public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener {
  
      //Initialize views in Viewholder
      TextView mTextViewFirst;
      TextView mTextViewSecond;
      ImageView mImageView;
      TextView mEndPercentageTextView;
  
      //Context to refer to app context (for intent, dialog etc)
      Context context;
  
      //Adapter to notifiy data set changed
      RecyclerViewCourseAdapter mCourseAdapter;
  
  
      public ViewHolder(View itemView, Context context, RecyclerViewCourseAdapter mCourseAdapter) {
  
          super(itemView);
          this.context = context;
          this.mCourseAdapter = mCourseAdapter;
          itemView.setOnClickListener(this);
          itemView.setOnLongClickListener(this);
          mTextViewFirst = (TextView) itemView.findViewById(R.id.course_firstLine);
          mTextViewSecond = (TextView) itemView.findViewById(R.id.course_secondLine);
          mImageView = (ImageView) itemView.findViewById(R.id.icon);
          mEndPercentageTextView = (TextView) itemView.findViewById(R.id.end_percentage_textview);
      }
  </code></pre>
  
  <p>This is how i create the Viewholders:</p>
  
  <pre><code>public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      //Inflate layout
      View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_course, parent, false);
      ViewHolder vh = new ViewHolder(itemView, context, this);
  
      return vh;
  }
  </code></pre>
  
  <p>So i create my ViewHolder with the context stored in the Adapter.</p>
  
  <p>If something is still unlcear, here is my whole class: 
  <a href="https://github.com/Tak3r07/UniHelper/blob/master/app/src/main/java/com/tak3r07/CourseStatistics/RecyclerViewCourseAdapter.java" rel="nofollow">https://github.com/Tak3r07/UniHelper/blob/master/app/src/main/java/com/tak3r07/CourseStatistics/RecyclerViewCourseAdapter.java</a></p>