如何显示然后删除android进度对话框

Bri*_*ian 4 android progressdialog

我可以使用以下代码显示进度条

pd = ProgressDialog.show(myActivity.this, "", "Loading. Please wait...", true);
Run Code Online (Sandbox Code Playgroud)

直截了当,但是一旦我有代码执行,我希望它消失,但是当我在看不到对话框显示之后运行dismiss方法时.

下面是包含在oncreate中的上下文中的代码

pd = ProgressDialog.show(myActivity.this, "", "Loading. Please wait...", true);
runCode();  
setListAdapter(new CustomAdapter(myActivity.this)); 
pd.dismiss();
Run Code Online (Sandbox Code Playgroud)

我以为你可以在活动的任何地方显示/解除进度对话框,但我一定是错的.

Bri*_*ian 12

这是我开始工作的代码

private class UpdateFeedTask extends AsyncTask<MyActivity, Void, Void> {

    private ProgressDialog mDialog;

    protected void onPreExecute() {
        Log.d(TAG, " pre execute async");
        mDialog = ProgressDialog.show(MyActivity.this,"Please wait...", "Retrieving data ...", true);

    }

    protected void onProgressUpdate(Void... progress) {
        Log.d(TAG, " progress async");     
    }

    @Override
    protected Void doInBackground(MyActivity... params) {
        // TODO Auto-generated method stub
        return null;
    }

    protected void onPostExecute(Void result) {
        Log.d(TAG, " post execute async");
        mDialog.dismiss();
    }

}
Run Code Online (Sandbox Code Playgroud)