1

当用户单击按钮时,我想在我的页面上显示一个progressDialog。单击按钮时,我正在对列表的结果进行排序。现在我们如何在单击按钮时显示progressDialog。请建议我

我用来对该数据进行排序的此功能:

public void sortByDate(View v) {
        Collections.sort(tripParseData.getDetails());
        setData(tripParseData);
    }

@Monica建议之后

public void sortByDate(View v) {
        new LoadData().execute();

    }

class LoadData extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            Collections.sort(tripParseData.getCoroprateBookingDetails());
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            progressDialog.dismiss();
            setApprovalDetailsData(tripParseData);
        }
    }
4

4 回答 4

1
    Paste This Class in ur activity and call new LoadData().execute to start the prgress dialog :
    class LoadData extends AsyncTask<Void, Void, Void> {
    @Override
            protected void onPreExecute() {

          super.onPreExecute();  
          ProgressDialog pg=new ProgressDialog(ChartsActivity.this);
          pg=pg.show(ChartsActivity.this, "Loding", "Plz Wait...");
            }



 @Override
protected Void doInBackground(Void... params) {
sortByDate();
    }

    @Override
        protected void onPostExecute(Void result) {      
        super.onPostExecute(result);
        pg.dismiss();
    }
    }
于 2013-10-09T10:49:39.157 回答
1

您必须在长计算开始之前调用progressdiolog.show,然后计算必须在单独的线程中运行。一旦这个线程完成,你必须调用 pd.dismiss() 来关闭 prgoress 对话框。

在这里你可以看到一个例子:

进度对话框被创建和显示,并调用一个线程来运行一个繁重的计算:

Override
    public void onClick(View v) {
       pd = ProgressDialog.show(lexs, "Search", "Searching...", true, false);
       Search search = new Search(   ...   );
       SearchThread searchThread = new SearchThread(search);
       searchThread.start();
    }

这里是线程:

private class SearchThread extends Thread {

        private Search search;

        public SearchThread(Search search) {
            this.search = search;
        }

        @Override
        public void run() {         
            search.search();
            handler.sendEmptyMessage(0);
        }

        private Handler handler = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                displaySearchResults(search);
                pd.dismiss();
            }
        };
    }

别忘了给我投票:)

于 2013-10-09T10:52:39.507 回答
1

我没有测试过,但我认为它可以帮助你。

在你的类和成员变量中声明这两个对象。

Thread thread = null;
ProgressDialog bar = null;

在您的按钮单击侦听器上使用此逻辑,它可以在您的情况下工作。我还没有测试过它,但它会让你知道事情应该如何工作。您还可以使用 handlemessage 代替 runonuithread

    if (thread == null
     || (thread != null && !thread.isAlive())) { 
        thread = new Thread(new Runnable() {

            @Override
            public void run() {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        //DISPLAY YOUR PROGRESS BAR HERE.
                        bar = ProgressDialog.show(getApplicationContext(), "LOADING..", "PLEASE WAIT..");
                    }
                });
                // SORT COLLECTION HERE
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        //CLOSE YOUR PROGRESS BAR HERE.
                        bar.dismiss();
                    }
                });
                }
            });
            thread.start();
        }

希望这会帮助你。

于 2013-10-09T11:14:29.277 回答
0

尝试使用 AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html

示例代码:

    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

        @Override
        protected void onPreExecute() {
             //Show UI
             //Start your progress bar

             showProgress();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
             // do your sorting process 
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
                          //Show UI
             //dismiss your progress bar
            hideProgress();
        }

    };
task.execute((Void[])null);

显示和隐藏进度代码

public void showProgress() {
        progressDialog = ProgressDialog.show(this, "",
                "Loading. Please wait...");
        progressDialog.setCancelable(false);
    }

    public void hideProgress() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
于 2013-10-09T10:52:57.883 回答