title: Using custom dialog box in custom listview button click
body: <p>I am working on an android project where I have to fetch data from server and display it in a custom listview along with a button, I have fetched and showed the listview but I want to start a dialog box whenever the button in the each row of the listview is clicked. I am stucked on how to initiate the call from the button press, I have tried in some ways but doesn't work.. My codes are pasted below, any help would be greatly appreciated..</p>
  
  <p>The contactAdapter class of my code is given below:</p>
  
  <pre><code>package com.example.sohan.doctor;
  
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.app.Dialog;
  import android.app.FragmentManager;
  import android.content.Context;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.v4.app.FragmentActivity;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ArrayAdapter;
  import android.widget.BaseAdapter;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  import android.app.AlertDialog;
  import android.app.Dialog;
  import android.content.DialogInterface;
  import android.support.v4.app.DialogFragment;
  import java.util.ArrayList;
  import java.util.List;
  
  
  /**
   * Created by Sohan on 6/9/2016.
   */
  public class ContactAdapter extends ArrayAdapter&lt;Contacts&gt; {
     // notification nt = new notification();
      List&lt;Contacts&gt; list = new ArrayList&lt;Contacts&gt;();
      View row;
      ContactHolder contactHolder;
      LayoutInflater inflater;
      View v;
      private Activity activity;
  
      public ContactAdapter(Context context, int resource) {
          super(context, resource);
      }
  
  
  
      public void add(List&lt;Contacts&gt; updatedList) {
          list.clear();
          list.addAll(updatedList);
          notifyDataSetChanged();
      }
  
  
      @Override
      public Contacts getItem(int position) {
          return list.get(position);
      }
  
      @Override
      public int getCount() {
          return list.size();
      }
  
      @Override
      public View getView(final int position, View convertView, ViewGroup parent) {
          row = convertView;
  
          if(row==null){
              LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              row = layoutInflater.inflate(R.layout.view_symptom_layout,parent,false);
              contactHolder = new ContactHolder();
              contactHolder.Name =(TextView) row.findViewById(R.id.textView2);
              contactHolder.Age =(TextView) row.findViewById(R.id.textView3);
              contactHolder.Height =(TextView) row.findViewById(R.id.textView4);
              contactHolder.Weight =(TextView) row.findViewById(R.id.textView5);
              contactHolder.Symptom =(TextView) row.findViewById(R.id.textView6);
              contactHolder.button = (Button) row.findViewById(R.id.button3);
              contactHolder.button.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {               // this is the onClick method for the custom button
                      String name;
  //                    name = list.get(position).getName();
  //                    Toast.makeText(getContext().getApplicationContext(), name+" is served ", Toast.LENGTH_LONG).show();
  //                    Toast.makeText(getContext().getApplicationContext(), "Position of "+name+" is "+position, Toast.LENGTH_LONG).show();
                        callDialog cd = new callDialog();
                        cd.showBox();
                        //android.support.v4.app.FragmentManager fm = cd.getSupportFragmentManager();
                        //md.show(, "my_dialog");
  
                      //---------  another solution
  //                    inflater = getActivity().getLayoutInflater();
  //                    v = inflater.inflate(R.layout.custom_dialogbox, null);
  //                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  //                    builder.setView(v).setPositiveButton("Send Treatment", new DialogInterface.OnClickListener() {
  //                        @Override
  //                        public void onClick(DialogInterface dialog, int which) {
  //
  //                        }
  //                    });
  //                    builder.show();
                      //---------- another solution
                  }
              });
  
              row.setTag(contactHolder);
          }
          else{
  
              contactHolder = (ContactHolder)row.getTag();
          }
  
  
  
          Contacts contacts = (Contacts)this.getItem(position);
          contactHolder.Name.setText("Name: "+contacts.getName());
          contactHolder.Age.setText("Age: "+contacts.getAge());
          contactHolder.Height.setText("Height: "+contacts.getHeight());
          contactHolder.Weight.setText("Weight: "+contacts.getWeight());
          contactHolder.Symptom.setText("Symptoms: " + contacts.getSymptom());
          return row;
  
      }
  
      public Activity getActivity() {
          return activity;
      }
  
      static class ContactHolder{
          TextView Name;
          TextView Age;
          TextView Height;
          TextView Weight;
          TextView Symptom;
          Button button;
      }
  
  
            class callDialog extends FragmentActivity {
  
              public void showBox(){
                  MyDialog md = new MyDialog();
                  md.show(getSupportFragmentManager(), "my_dialog_box");
              }
          }
  
  
  
  }
  </code></pre>
  
  <p>The dialog box code are given below:</p>
  
  <pre><code>package com.example.sohan.doctor;
  
  import android.app.AlertDialog;
  import android.app.Dialog;
  import android.content.DialogInterface;
  import android.os.Bundle;
  import android.support.annotation.NonNull;
  import android.support.v4.app.DialogFragment;
  import android.view.LayoutInflater;
  import android.view.View;
  
  /**
   * Created by Sohan on 7/13/2016.
   */
  public class MyDialog extends DialogFragment {
  
      LayoutInflater inflater;
      View v;
      public Dialog  onCreateDialog(Bundle savedInstanceState) {
          inflater = getActivity().getLayoutInflater();
          v = inflater.inflate(R.layout.custom_dialogbox, null);
          AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
          builder.setView(v).setPositiveButton("Send Treatment", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
  
              }
          });
          return builder.create();
      }
  }
  </code></pre>