当twitter提要加载时,我试图显示一个进度对话框…但是当twitter提要出现时,进度对话框仍保留在屏幕上。任何帮助都非常感谢。

public class MainActivity extends ListActivity {

    final static String twitterScreenName = "CFABUK";
    final static String TAG = "MainActivity";
    private AsyncTask<Object, Void, ArrayList<TwitterTweet>> tat;
    boolean done;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        done=false;
        AndroidNetworkUtility androidNetworkUtility = new AndroidNetworkUtility();
        if (androidNetworkUtility.isConnected(this)) {
            TwitterAsyncTask syn=new TwitterAsyncTask();
            syn.execute(twitterScreenName,this);


            ProgressDialog pd = new ProgressDialog(MainActivity.this);
            pd.setMessage("loading");
            pd.show();

            do {
                if(!(syn.getStatus()==AsyncTask.Status.RUNNING)) {
                pd.dismiss();
                pd.cancel();
                done=true;
                }
            } while(done=false);


        } else {
            Log.v(TAG, "Network not Available!");
        }
    }
}

最佳答案

必须对异步任务onPreExecute()调用progressDialog show()方法。例如:

class MyTask extends AsyncTask<Void, Void, Void> {
 ProgressDialog pd;
    @Override
    protected void onPreExecute() {
      super.onPreExecute();
       pd = new ProgressDialog(MainActivity.this);
       pd.setMessage("loading");
       pd.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
      // Do your request
    }

    @Override
    protected void onPostExecute(Void result) {
      super.onPostExecute(result);
      if (pd != null)
      {
         pd.dismiss();
      }
    }
  }

07-27 17:25