title: Creating and Showing an AlertDialog from a custom ListAdapter
body: <p>I am implementing a custom ListAdapter which is using different list item layouts to show some items. From that custom ListAdapter, I actually want to show an AlertDialog when a particular Button is clicked. I implemented the onCreateDialog(int) method and I am trying to use showDialog(int) to show the dialog. But the dialog does not show up in the Activity.</p>
  
  <p>Here is my custom listadapter file</p>
  
  <pre><code>
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.app.Dialog;
  import android.content.DialogInterface;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class AddProblemsLayoutAdapter extends BaseAdapter {
  
      private Activity mContext;
      private static final int TYPE_TITLE = 0;
      private static final int TYPE_TAG = 1;
      private static final int TYPE_SOLUTION = 2;
      private static final int LAYOUT_MAX_COUNT = TYPE_SOLUTION + 1;
      private static final int ADD_TAG_DIALOG = 3378;
      private static int ITEM_COUNT = 4;
      private static Button addSolution = null, addTag = null;
  </code></pre>
  
  <pre><code>private Activity mContext;
  private static final int TYPE_TITLE = 0;
  private static final int TYPE_TAG = 1;
  private static final int TYPE_SOLUTION = 2;
  private static final int LAYOUT_MAX_COUNT = TYPE_SOLUTION + 1;
  private static final int ADD_TAG_DIALOG = 3378;
  private static int ITEM_COUNT = 4;
  private static Button addSolution = null, addTag = null;
  
  
  public AddProblemsLayoutAdapter(Activity aContext) {
  
      mContext = aContext;
  }
  
  @Override
  public int getViewTypeCount() {
      return LAYOUT_MAX_COUNT;
  }
  
  protected Dialog onCreateDialog(int id) {
      AlertDialog dialog = null;
      AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
      switch (id) {
      case ADD_TAG_DIALOG:
          builder.setMessage("Are you sure you want to exit?").setCancelable(
                  false).setPositiveButton("Yes",
                  new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int id) {
  
                      }
                  }).setNegativeButton("No",
                  new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int id) {
                          dialog.cancel();
                      }
                  });
  
          dialog = builder.create();
          break;
      default:
          dialog = null;
      }
      dialog.setOwnerActivity(mContext);
      return dialog;
  }
  
  @Override
  public int getItemViewType(int position) {
      if (position &lt; 2)
          return TYPE_TITLE;
      else
          return position &gt; 2 ? TYPE_SOLUTION : TYPE_TAG;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      int type = getItemViewType(position);
      if (convertView == null) {
          LayoutInflater inflater = mContext.getLayoutInflater();
          switch (type) {
          case TYPE_TITLE:
              convertView = inflater.inflate(R.layout.title_row, null);
              break;
  
          case TYPE_TAG:
              convertView = inflater.inflate(R.layout.tag_row, null);
              break;
          case TYPE_SOLUTION:
              convertView = inflater.inflate(R.layout.solution_row, null);
              break;
          }
      }
      if (position == 0) {
          TextView titleText = (TextView) convertView
                  .findViewById(R.id.titleText);
          titleText.setText(R.string.title_string);
      } else if (position == 1) {
          TextView titleText = (TextView) convertView
                  .findViewById(R.id.titleText);
          titleText.setText(R.string.description_string);
      } else if (position == 2) {
          addTag = (Button) convertView.findViewById(R.id.addProblemTag);
          addTag.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  mContext.showDialog(ADD_TAG_DIALOG);
                  Toast.makeText(mContext, "Add Tags", Toast.LENGTH_LONG)
                          .show();
              }
          });
  
      } else if (position == 3) {
          addSolution = (Button) convertView.findViewById(R.id.addSolution);
          addSolution.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  ITEM_COUNT++;
                  notifyDataSetChanged();
              }
          });
      }
      return convertView;
  }
  
  @Override
  public int getCount() {
      return ITEM_COUNT;
  }
  
  @Override
  public Object getItem(int position) {
      // TODO Auto-generated method stub
      return null;
  }
  
  @Override
  public long getItemId(int position) {
      // TODO Auto-generated method stub
      return 0;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Could anyone give me some pointers as to how to show the AlertDialog box on the button click.</p>