gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:27:45 24 4
gpt4 key购买 nike

我的页面中有搜索框,我也添加了 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com