我在我的頁面中有搜索框,我也添加了onTextChangeListener()。當我在搜索框中添加/刪除字母/字符時,每次我調用相同的asyntask.Suppose 3次調用,然後3次進度對話框實例創建並需要解散。這裏的進度對話框沒有被解僱。解除asyntask中的進度對話框
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();
}
你可以請upvote它 – Sush