2011-03-01 31 views
1

我有一個從服務器獲取數據的AsyncTask。我需要的是在對話框中顯示重新獲取的數據。我怎樣才能調用函數來在AsyncTask中創建對話框?如果我不能這樣做,那麼我有什麼選擇?是否有可能知道AsyncTask是否已完成?是否可以從AsyncTask中調用另一個組合?

這裏是我的代碼:

//Function calling the AsyncTask 
public void getData(int pos) 
{ 
new DBTask().execute(); 
//Is it possible to know here if the AsyncTask has ended or not?? 
} 

private class DBTask extends AsyncTask<Long, Boolean, Integer>{ 
    ProgressDialog ServerPD = new ProgressDialog(MRRankingActivity.this); 

    @Override 
    protected void onPreExecute() 
    { 
     ServerPD = ProgressDialog.show (MRRankingActivity.this, "", "Connecting to server...", true, false); 
    }//End of onPreExecute 

    @Override 
    protected Integer doInBackground(Long... params) 
    { 
     publishProgress(isOnline()); 

      if(isOnline()) 
      { 
      getDBData(); 
        if(isOK) 
         { 
        //I want to show the result by calling this function.. 
         showResult(); 
//Calling this function will trow error..So how can i show my result?? 
         Log.d("LIST","DONE..Reached Final Destination"); 

         } 
      } 
     } 

     return null; 
    } 

     @Override 
    protected void onProgressUpdate(Boolean... isConnection) { 
    // TODO Auto-generated method stub 

    super.onProgressUpdate(isConnection); 
    if(isOnline()) 
      { 
      ServerPD.setMessage("Retreving Data"); 
      } 
    } 

     @Override 
    protected void onPostExecute(Integer result) { 
     if (ServerPD.isShowing()) 
     { 
      ServerPD.dismiss(); 
     } 


    }//End of onPostExecute 
}//End of DBTask 

public void getDBData() 
{ 

    try{ 
    //Code foe connecting to database and retreive data. 
    //If everything is ok then isOK is set to True.. 
    //else its set to false  

     isOK=true; 
      } 
    catch(Exception e) 
     { 
      isOK=false; 
     }//End if catch 
} 

public void showResult() 
{ 
    //I want to show result here in a new Dialog ... 
} 

//Checking is connection available 
public boolean isOnline() { 
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
    if (netInfo != null && netInfo.isConnectedOrConnecting()) { 
     return true; 
    } 
    return false; 
} 

我怎樣才能顯示結果在這種情況呢?有什麼方法可以查找AsyncTask是否結束?

回答

1

簡化什麼馬修suggested..just修改這樣的代碼 首先通過初始化一個int varable isSuccess修改doInBackgroud和然後在isOK if語句中將isSucces的值更改爲1。現在返回isSuccess。這樣

@Override 
    protected Integer doInBackground(Long... params) 
    { 
     int isSuccess=0; 
     publishProgress(isOnline()); 

      if(isOnline()) 
      { 

      getDBData(); 
        if(isOK) 
         { 
         isSuccess=1; 
         } 
      } 
     } 

     return isSuccess; 
    } 

現在修改您的PostExecute()在onPostExecute

@Override 
    protected void onPostExecute(Integer result) { 
     if (ServerPD.isShowing()) 
     { 
      ServerPD.dismiss(); 
     } 
     if(result==1) 
      { 
       showResult(); 
      } 

    }//End of onPostExecute 

希望這有助於處理ruturned作爲結果isSuccess值。

+0

謝謝..你讓它看起來很簡單 – Shijilal

1

而不是給

private class DBTask extends AsyncTask<Long, Boolean, Integer>{ 

private class DBTask extends AsyncTask<Long, Boolean, Boolean>{ 
內部doInBackground

則()創建一個變量:

boolean success = false;

內UR ifOnline()使:

success = true; 

而在這方法到底能給:

return success; 

裏面你onPostExecute()取參數爲布爾值,而不是整數。

給:

if(result) { 

showResult(); 

} 

希望這可以幫助你。 onPostExecute()僅在doInBackground()後被調用。

+0

謝謝...這很有用 – Shijilal

0

Shijilal ...當doInBackground方法結束時,您會在onPostExecute中通知您。 doInBackground中的返回值被髮送到onPostExecute。你不應該觸摸在與UI線程分離的線程中運行的doInBackground中的UI。

我想在調用asynchTask.execute之前調用isOnline。我會認爲doInBackground中的方法會返回類型Result,然後將其發送到onPostExecute。所以考慮:AsynchTask<SQLQuery,Void,ResultSet>或其他適當的。

1

達到目的的最佳方法是創建一個Handler並在其中顯示對話框。一些這樣的事

class RefreshHandler extends Handler { 

    @Override 

    public void handleMessage(Message msg) { 

AlertDialog.Builder alertbox = new AlertDialog.Builder(
          CardDetails.this); 
        alertbox.setMessage("Please Login First ..."); 
        alertbox.setPositiveButton("Ok", 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface arg0, 
             int arg1) { 
           } 
          }); 
        alertbox.show(); 

    } 
} 

,並從你的異步任務做到這一點

new RefreshHandler().sendEmptyMessage(0)

相關問題