title: How to insert loading dialog on clicking an item of listview
body: <p>I have a listactivity. When i click an item of this list activity, the activity called takes time to load and hence i want to add a loading dialog box which should disappear as soon as the activity starts. But I am not able to do it. 
  Also I am confused as to add the dialog in list activity or the called activity.
  On adding the dialog to latter activity, I get this error:</p>
  
  <p><code>android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@40521cd0 is not valid; is your activity running?</code></p>
  
  <p>This is how I insert the dialog</p>
  
  <pre><code>    super.onCreate(savedInstanceState);         
          setContentView(R.layout.graphview);     
          ProgressDialog pdialog=new ProgressDialog(this);
           pdialog.setCancelable(true);
           pdialog.setMessage("Loading ....");
           pdialog.show();
  </code></pre>
  
  <p>Please tell me what is going wrong.</p>
  
  <p>Here is my complete code:</p>
  
  <pre><code> public class List12 extends ListActivity {
  
       private List&lt;String&gt; item = null;
       private List&lt;String&gt; path = null;
       private String root;
       private TextView myPath;
       private ProgressDialog mProgressDialog;
       public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          final Bundle b=this.getIntent().getExtras(); 
          final String path=(String) b.get("key"); 
          setContentView(R.layout.list13);
          myPath = (TextView)findViewById(R.id.path);
  
  
              root = Environment.getExternalStorageDirectory().getPath()+"/"+path+"/raw/";        
  
              File f = new File(root);
              File[] files = f.listFiles();
              if(files==null)
              {
                  Toast.makeText(this, "No stored records for the patient", Toast.LENGTH_LONG).show();
                  List12.this.finish();
              }
              // TODO Auto-generated catch block
              else
              getDir(root, path);
  
      }
  
      private void getDir(String dirPath, String id)
      {
       myPath.setText("Stored data for "+id);
       item = new ArrayList&lt;String&gt;();
       path = new ArrayList&lt;String&gt;();
       File f = new File(dirPath);
       File[] files = f.listFiles();
  
       if(!dirPath.equals(root))
       {
        item.add(root);
        path.add(root);
        item.add("../");
        path.add(f.getParent()); 
       }
  
       for(int i=0; i &lt; files.length; i++)
       {
        File file = files[i];
  
        if(!file.isHidden() &amp;&amp; file.canRead()){
         path.add(file.getPath());
            if(file.isDirectory()){
             item.add(file.getName() + "/");
            }else{
             item.add(file.getName());
            }
        } 
       }
  
       ArrayAdapter&lt;String&gt; fileList =
         new ArrayAdapter&lt;String&gt;(this, R.layout.row, item);
       setListAdapter(fileList); 
      }@Override
       protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        File file = new File(path.get(position)); 
        Bundle b=new Bundle();    
        String array = file.getAbsolutePath();
        b.putString("key",array); 
        b.putBoolean("flag", false);
        Intent in = new Intent(getParent(), Display.class);
        TabGroupActivity parentActivity = (TabGroupActivity)getParent();
        in.putExtras(b);  
        parentActivity.startChildActivity("Dsiplay", in);
        ProgressDialog pdialog=new ProgressDialog(List12.this);
        pdialog.setCancelable(true);  
      pdialog.setMessage("Loading ...."); 
        pdialog.show();
       }}
  </code></pre>