在asyntask中解除进度对话框

时间:2017-04-03 11:47:50

标签: android progressdialog

我的页面中有搜索框,我也添加了onTextChangeListener()。当我在搜索框中添加/删除字母/字符时,每次我调用相同的asyntask.Suppose 3次调用然后3次进度对话框实例被创建并需要被解雇。这个进度对话框没有被解雇。

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

    }

6 个答案:

答案 0 :(得分:2)

只需创建一个方法,如:

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

}

showProgressDialog 方法调入 onPreExecute()

要关闭对话框:

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

答案 1 :(得分:2)

// 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; 

答案 2 :(得分:0)

AsynchTask onPreExecute()方法中,首先检查进度对话框,如果它为null,则初始化并显示它。

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

答案 3 :(得分:0)

此问题的原因是:

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
        }
   }
}

希望它适合你。

答案 4 :(得分:0)

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

答案 5 :(得分:-1)

在您想要关闭对话框的位置使用此项。

ProgressDialog pDialog = new ProgressDialog(this);
pDialog.dismiss();