如何在单击列表视图项时插入加载对话框

时间:2012-11-06 07:52:38

标签: android progressdialog

我有一个列表活动。当我单击此列表活动的项目时,调用的活动需要时间加载,因此我想添加一个加载对话框,一旦活动开始,该对话框就会消失。但我无法做到。 另外,我很困惑,在列表活动或被调用的活动中添加对话框。 在将对话框添加到后一个活动时,我收到此错误:

android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@40521cd0 is not valid; is your activity running?

这是我插入对话框的方式

    super.onCreate(savedInstanceState);         
        setContentView(R.layout.graphview);     
        ProgressDialog pdialog=new ProgressDialog(this);
         pdialog.setCancelable(true);
         pdialog.setMessage("Loading ....");
         pdialog.show();

请告诉我出了什么问题。

这是我的完整代码:

 public class List12 extends ListActivity {

     private List<String> item = null;
     private List<String> 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<String>();
     path = new ArrayList<String>();
     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 < files.length; i++)
     {
      File file = files[i];

      if(!file.isHidden() && file.canRead()){
       path.add(file.getPath());
          if(file.isDirectory()){
           item.add(file.getName() + "/");
          }else{
           item.add(file.getName());
          }
      } 
     }

     ArrayAdapter<String> fileList =
       new ArrayAdapter<String>(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();
     }}

2 个答案:

答案 0 :(得分:2)

尝试使用AsyncTask作为子类

class yourFileOperationTask extends AsyncTask<String, String, String> {

 @Override
 protected void onPreExecute(String result) {
 //create and show your progress dialog here
    }
 @Override
 protected String doInBackground(String... params) {  
 // Do the stuff here(your file operation)
  }  
 @Override
 protected void onPostExecute(String result) {
 //dismiss the dialog and  start the intent.
    }
}

然后在您的onclick中执行此操作:

//note that you can change the parameter types of your AsyncTask
new yourFileOperationTask().execucte(stringArrayParameter);

答案 1 :(得分:0)

将进度对话框放在单击侦听器中 例如:

public class MyList extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    Button btn  = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ProgressDialog pdialog=new ProgressDialog(MyList.this);
             pdialog.setCancelable(true);
             pdialog.setMessage("Loading ....");
             pdialog.show();
        //Rest of the code
        }
    });
}

}