title: How to access fragment variable from other class
body: <p>I have a Fragment <code>NewsFragment</code>in which I define an <code>AlertDialog</code>. I want to access the <code>AlertDialog</code> to close it after on click on the item. However I cannot access it. </p>
  
  <p>I found this code on the internet and it works fine but with an Activity not with a Fragment.</p>
  
  <pre><code>((NewsFragment) context).alertDialogNews.cancel();
  
  public class OnItemClickListenerListViewItem implements OnItemClickListener {
  
      @Override
      public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  
          //Context context = view.getContext();
          Context context = view.getContext();
  
          TextView textViewItem = ((TextView) view.findViewById(R.id.textViewItem));
  
          // get the clicked item name
          String listItemText = textViewItem.getText().toString();
  
          // get the clicked item ID
          String listItemId = textViewItem.getTag().toString();
  
          // just toast it
          // Toast.makeText(context, "Item: " + listItemText + ", Item ID: " + listItemId, Toast.LENGTH_SHORT).show();
          //((MainActivity) context).alertDialogStores.cancel();
          ((NewsFragment) context).alertDialogNews.cancel();
      }
  }
  </code></pre>