我的页面中有搜索框,我也添加了 onTextChangeListener() 。当我从搜索框中添加/删除字母/字符时,每次我调用相同的asyntask。假设调用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();
}
最佳答案
// 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;
关于android - 关闭异步任务中的进度对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43183844/