本文介绍了AsyncTask 的进度对话框 - Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to show a progress dialog while the twitter feed is loading up...However the progress dialog remains on screen when the twitter feed appears. Any help is much appreciated.

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!");
        }
    }
}

推荐答案

您必须在 AsyncTasks onPreExecute() 上调用 ProgressDialog show() 方法.例如:

You must call ProgressDialog show() method on AsyncTasks onPreExecute(). For example:

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();
      }
    }
  }

这篇关于AsyncTask 的进度对话框 - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 22:53