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

问题描述

我想显示一个进度对话框,而Twitter的饲料被加载了......但是进度对话框显示在屏幕上Twitter的饲料出现时。任何帮助深表AP preciated。

 公共类MainActivity扩展ListActivity {    最终静态字符串twitterScreenName =CFABUK;
    最终静态字符串标记=MainActivity;
    私人AsyncTask的<对象,无效的ArrayList< TwitterTweet>>达;
    布尔做的;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        做= FALSE;
        AndroidNetworkUtility androidNetworkUtility =新AndroidNetworkUtility();
        如果(androidNetworkUtility.isConnected(本)){
            TwitterAsyncTask SYN =新TwitterAsyncTask();
            syn.execute(twitterScreenName,这一点);
            ProgressDialog PD =新ProgressDialog(MainActivity.this);
            pd.setMessage(装载);
            pd.show();            做{
                如果(!(syn.getStatus()== AsyncTask.Status.RUNNING)){
                pd.dismiss();
                pd.cancel();
                做= TRUE;
                }
            }而(做= FALSE);
        }其他{
            Log.v(TAG,网络不可用!);
        }
    }
}


解决方案

您必须调用ProgressDialog秀上preExecute AsyncTasks()()方法。例如:

 类MyTask扩展的AsyncTask<太虚,太虚,太虚> {
 ProgressDialog PD;
    @覆盖
    在preExecute保护无效(){
      super.on preExecute();
       PD =新ProgressDialog(MainActivity.this);
       pd.setMessage(装载);
       pd.show();
    }    @覆盖
    保护无效doInBackground(虚空...... PARAMS){
      //做你的要求
    }    @覆盖
    保护无效onPostExecute(虚空结果){
      super.onPostExecute(结果);
      如果(PD!= NULL)
      {
         pd.dismiss();
      }
    }
  }

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!");
        }
    }
}
解决方案

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:55