2017-04-03 93 views
2

我在我的頁面中有搜索框,我也添加了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(); 

    } 

回答

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; 
+0

你可以請upvote它 – Sush

1

只是創建一個方法,如:

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

這將是很好解釋爲什麼這段代碼工作,有什麼問題,SO與他的代碼。 – danypata

+0

@danypata ** if(progressDialog == null)**在這裏,我們檢查是否有任何正在運行的進程對話框實例。如果對話框已經在運行,則不會創建新的對話框實例。通過這種方式,一次只能運行一個實例。 –

-1

使用此要解僱對話框。

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

在對進度對話框你AsynchTaskonPreExecute()方法首先檢查它是否爲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; 
     } 
     } 
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 
     } 
    } 
} 

希望它適用於你。

+0

他不能取消一旦創建的對話,如果你可以看到'setCancelable(false);'所以他必須等待的結果,然後將被解僱。 –

+0

'setCancelable(false);'方法設置該對話框是否可用BACK鍵取消。請訪問此處:https://developer.android.com/reference/android/app/Dialog.html#setCancelable(boolean) –

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