取消异步任务中的进度对话框

dismiss progress dialog in asyntask

我的页面中有搜索框,我也添加了 onTextChangeListener()。当我从搜索框 add/removing letters/characters 时,每次我调用相同的 asyntask.Suppose 3 次然后调用 3 次进度对话框实例并需要 dismissed.Here 进度对话框是没有被解雇。

search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                searchresult = search.getText().toString();

                if (count == 0) {
                    pageCount = 1;
                    productsList.clear();
                    mCount = 0;
                    if (isTextWatcherVisible) {
//                        GetProductsTask();
                        new GetProductsTask().execute();
                    }
                }
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        });   
class GetProductsTask extends AsyncTask<Void, Void, Void> {

    final String pcid = Preferences.getString(Preferences.PrefType.enum_company_id, getActivity());

    String url1 = getContext().getString(R.string.url) + "products?company_id=" + pcid + "&page=" + pageCount;

    public GetProductsTask() {
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("Loading...");
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(false);
    }
    protected void onPreExecute() {
        super.onPreExecute();
//                progressDialog = new ProgressDialog(getActivity());
//                progressDialog.setMessage("Loading...");
//                progressDialog.setIndeterminate(false);
//                progressDialog.setCancelable(false);
        progressDialog.show();

    }

    @Override
    protected Void doInBackground(Void... params) {
      // somthig here
    }
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        ProductslistAdapter adapter = new ProductslistAdapter(getActivity(), R.layout.vendor_products_item, productsList, searchresult);

        list.setAdapter(adapter);
        int count = pageCount - 1;
        list.setSelection(count * 24);
        if (progressDialog.isShowing())
            progressDialog.dismiss();

    }

只需创建一个方法,例如:

private static ProgressDialog progressDialog;

public static void showProgressDialog(Context context) {

        if (progressDialog == null) {
            progressDialog = new ProgressDialog(getActivity());
            progressDialog.setMessage("Loading...");
            progressDialog.setIndeterminate(false);
            progressDialog.setCancelable(false);
            progressDialog.show();
        }

}

调用 显示进度对话框 方法进入 onPreExecute().

关闭对话框:

if(progressdailog.isShowing()){
    progressdialog.dismiss();
}

在您想关闭对话框的地方使用它。

ProgressDialog pDialog = new ProgressDialog(this);
pDialog.dismiss();
// here your everytime your creating the dialog box, loosing previous reference. And "progressDialog " is not local to class "GetProductsTask".     
   progressDialog = new ProgressDialog(getActivity());
    progressDialog.setMessage("Loading...");
    progressDialog.setIndeterminate(false);
    progressDialog.setCancelable(false);

解决方案:

class GetProductsTask extends AsyncTask<Void, Void, Void> {
//make it local to class
ProgressDialog progressDialog; 

在您的 AsynchTask onPreExecute() 方法中,首先检查进度对话框是否为空,然后初始化并显示它。

protected void onPreExecute() {
        super.onPreExecute();
         if (progressDialog == null) {
            progressDialog = new ProgressDialog(getActivity());
            progressDialog.setMessage("Loading...");
            progressDialog.setIndeterminate(false);
            progressDialog.setCancelable(false);
            progressDialog.show();
        }
    }

然后在 onPostExecute() 方法中将其关闭。

protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Your other code goes here
        if(progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
                progressDialog = null;
         }
        }

导致此问题的原因是:

new GetProductsTask().execute();

每次您键入字符时,此语句都会创建新任务。所以你需要在取消当前任务后创建新任务。

public class MainActivity extends Activity {
   GetProductsTask getProductsTask = null;
   ProgressDialog ProgressDialog = null;


  public void onTextChanged(CharSequence s, int start, int before, int count) {
        //Replace this: new GetProductsTask().execute();
        if(getProductsTask != null) {
            getProductsTask.cancel();
            getProductsTask = null;
        }

        getProductsTask = new GetProductsTask();
        getProductsTask.execute();
   }

   class GetProductsTask extends AsyncTask<Void, Void, Void> {
        //Add new method inside it
        public void onCancelled (Result result) {
            //Got call when task is cancelled
        }
   }
}

希望对你有用。

更新 AsyncTask 如下:

class GetProductsTask extends AsyncTask<Void, Void, Void> {

    final String pcid = Preferences.getString(Preferences.PrefType.enum_company_id, getActivity());
    String url1 = getContext().getString(R.string.url) + "products?company_id=" + pcid + "&page=" + pageCount;

    ProgressDialog progressDialog;

    public GetProductsTask() {
        progressDialog = new ProgressDialog(getActivity());
    }

    protected void onPreExecute() {

        progressDialog.setMessage("Loading...");
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // somthig here

        return null;
    }

    protected void onPostExecute(Void result) {

        ProductslistAdapter adapter = new ProductslistAdapter(getActivity(), R.layout.vendor_products_item, productsList, searchresult);
        list.setAdapter(adapter);

        int count = pageCount - 1;
        list.setSelection(count * 24);

        if (progressDialog.isShowing())
            progressDialog.dismiss();
    }
}