简体   繁体   English

异步任务不显示按钮单击事件的进度对话框

[英]Asynchronous task not show Progress Dialog on click event of button

In my application when i click on Button it sometimes shows the progressdialog and sometimes not show the progressdialog on click of button. 在我的应用程序中,当我单击Button时,它有时会显示progressdialog,有时候不会在点击按钮时显示progressdialog。

Asynchronous Task code is: 异步任务代码是:

public class LoadData extends AsyncTask<Void, Void, Void>
    {
        ProgressDialog pd;
        @Override
        protected void onPreExecute()
        {
            pd = ProgressDialog.show(MainActivity.this, "", "Loading...");
        }
        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            MainActivity.this.runOnUiThread(new Runnable() {

                public void run() {
                    // TODO Auto-generated method stub
                    LoadActivities(); // function to load data from url 

                }
            });
            return null;
        }
        @Override
        protected void onPostExecute(Void unused) 
        {
            pd.dismiss();    
        }

    }

and on button click event call this as: 按钮点击事件将其调用为:

btn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        new LoadMoreData().execute(null);
    }
});  

the wrong think you are doing is that in doInBackground you use runOnUiThreade . 你错误的认为你在doInBackground使用runOnUiThreade just remove that from your code . 只需从代码中删除它。 It solves your problem. 它解决了你的问题。

never use any thread in doInBackground. 永远不要在doInBackground中使用任何线程。

Why you have taken run method again in doInBackground , doInBackground method performs computation on a background thread, so no need to take runOnUiThread 为什么你在doInBackground中再次使用run方法,doInBackground方法在后台线程上执行计算,所以不需要运行runOnUiThread

      MainActivity.this.runOnUiThread(new Runnable() {

            public void run() {
                // TODO Auto-generated method stub
                LoadActivities(); // function to load data from url 

            }
        });

Just write 写吧

protected Boolean doInBackground(final String... args) {
        try {
            LoadActivities(); 
            return true;
        } catch (Exception e) {
            Log.e("tag", "error", e);
            return false;
        }
    }

And also change new LoadMoreData().execute(); 并且还要更改new LoadMoreData().execute(); don't write null 不要写null

             btn.setOnClickListener(new View.OnClickListener() {      
                public void onClick(View v) {
                        new LoadMoreData().execute();
                }});   

Nirali's answer seems correct, just to make further explaination and some edits. Nirali的回答似乎是正确的,只是为了进一步解释和一些编辑。 Progress Dialog will be shown by the time doInBackground method returns value. 进度对话框将在doInBackground方法返回值时显示。 and in your code it just create another thread, and completes execution, so to display progress dialog by the time LoadActivities exectues, execute this statement in the same thread doInBackground executes, so change to following: 在你的代码中,它只是创建另一个线程,并完成执行,所以要在LoadActivities执行时显示进度对话框,在doInBackground执行的同一个线程中执行此语句,所以更改为以下内容:

@Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            MainActivity.this.runOnUiThread(new Runnable() {

            LoadActivities(); // function to load data from url 
            return null;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM