2013-02-05 105 views
0

我是android.os.AsyncTask處理android.app.ProgressDialog的新方法。我遇到了通過android.database.Cursor處理特定查詢並將其轉換爲我需要的實體的問題。Android ProgressDialog查詢處理

我正在關注此模板,但它總是會返回給我一個android.view.WindowManager$BadTokenException

List<Entity> loadData() throws Exception { 
    AsyncTask<Cursor, Void, List<Entity>> process = new AsyncTask<Cursor, Void, List<Entity>>() { 
     ProgressDialog dialog; 
     protected void onPreExecute() { 
      dialog = ProgressDialog.show(getApplicationContext(), "Please wait...", "Loading data...");   
     } 
     protected void onPostExcecute() { 
      dialog.dismiss(); 
     } 
     @Override 
     protected List<Entity> doInBackground(Cursor... params) { 
      List<Entity> entities = new ArrayList<Entity>(); 
      // process of convertion of data from android.database.Cursor to <pacakge>.Entity 
      return entities; 
     } 
    }.execute(/* the query : android.database.Cursor */) 
    return process.get(); 
} 

我錯過了什麼嗎?

回答

1

從的AsyncTask的onPreExecute()方法顯示ProgressDialog使用當前活動上下文中的AsyncTask是不是運行的getApplicationContext()爲:

dialog = ProgressDialog.show(Your_Current_Activity.this, 
        "Please wait...", 
        "Loading data..."); 

如果AsyncTask在單獨的類從Activity運行,那麼你就需要通過當前活動使用AsyncTask的類構造函數在AsyncTask中執行上下文

+0

ProgressDialog不顯示或者在進程結束時顯示。 –

+0

@ Dr.Java:但是這不是一個錯誤,因爲你正在使用AsyncTask的get方法,它會凍結UI直到doInBackground執行沒有完成。 –

+0

關於如何解決這個問題的任何建議? –