2017-07-27 86 views
0

這是我的一個活動的代碼片段,我想在android中實現AsyncTask。請告訴我,如果我錯了,我可以趕上/ retrove從doInBackgroundonPostExecute返回聲明,如果這是真的,我該怎麼辦呢?從AsyncTask的doInBackground方法中檢索return語句?

public class EvaluateTask extends AsyncTask{ 

    ProgressDialog progress = new ProgressDialog(context); 

    @Override 
    protected void onPreExecute() { 

     progress.setMessage("Analysing"); 
     progress.setIndeterminate(true); 
     progress.show(); 
    } 

    @Override 
    protected Object doInBackground(Object[] params) { 
     Evaluate evaluate = new Evaluate(db); //Evaluate class object 
     return evaluate.getScore(); 
    } 

    @Override 
    protected void onPostExecute(Object o) { 
     progress.dismiss(); 
     Intent i = new Intent(googleAct.this,result.class); 
     startActivity(i); 

     //ERROR IN FOLLOWING LINE >>>>>>> 
     i.putExtra("score",finalScore); 

    } 
} 

這裏請注意,我要通過getScore()方法,使用的AsyncTask(返回score變量)在背景的執行score變量從Evaluate類轉移到result活動。

+0

什麼是您的商店變量的類的類型。還有什麼是你得到額外的錯誤。由於你沒有任何在任何地方聲明爲finalscore的變量,它會給出錯誤。嘗試使用你的對象o –

+0

你需要閱讀更多關於'AsyncTask'的信息,當擴展類時缺少一些類型參數,但是doInbackground()中的返回值應該作爲參數傳遞給onPostexecute(),所以你可以檢索'onPostExecute(Object o)'中的參數'Object o'的值' – Yazan

回答

0

是的,如果正確寫入AsyncTask的類語句,我們可以從onPostExecute()的doInBackground()中檢索/捕獲return語句。

AsyncTask是一個接受三類類型(非原始)參數的泛型類。嘗試下面的代碼來理解並從doInBackground()方法獲取返回的值。

我期待您的Evaluate類的getScore()方法的返回類型是int。

public class EvaluateTask extends AsyncTask<Void,Void,Integer> { 

ProgressDialog progress = new ProgressDialog(context); 


@Override 
protected void onPreExecute() { 

    progress.setMessage("Analysing"); 
    progress.setIndeterminate(true); 
    progress.show(); 
} 

@Override 
protected Integer doInBackground(Void... params) { 
    Evaluate evaluate = new Evaluate(db); //Evaluate class object 
    return evaluate.getScore(); 
} 

@Override 
protected void onPostExecute(Integer finalScore) { 

    // in this method, parameter "finalScore" is the returned value from doInBackground() method, that you can use now. 
    super.onPostExecute(finalScore); 
    progress.dismiss(); 
    Intent i = new Intent(googleAct.this, result.class); 
    startActivity(i); 

    //I am considering you were expecting below finalScore as the returned value from doInBackground() method. 
    //Now there should not be an error. 
    i.putExtra("score", finalScore); 
} 
} 
+0

嘿@Manishoaham,它仍然在這一行顯示錯誤:'super.onPostExecute(integer); //錯誤整數字「@larry」。是的,'getScore()'返回整數類型。 –

+0

其實我先使用了變量「integer」,然後編輯它爲「finalScore」,所以忘了在這一行編輯它。如果您將其更改爲「finalScore」,則可以正常工作。如果對你有幫助,你也可以投票給答案。 :) – Manishoaham

+0

請檢查我所做的編輯,以便他人可以直接複製 - 使用正確的代碼。根據正確的代碼要求,我在'super.onPostExecute'方法中將'integer'更改爲'finalScore'。 –

1

當你擴展AsyncTask,你需要指定返回數據的類型,泛型的一部分:

public class EvaluateTask extends AsyncTask<DbData, Void, String> 

在這個例子中,我使用DbData來表示你正在使用什麼doInBackground()來檢索/評估數據,db。這應該適當地鍵入並作爲參數傳遞給任務。我還假設你想返回的score值是String。您可以更改爲您需要的對象類型(即,如果它是int,則使用Integer)。

無論你從doInBackground返回將提供作爲參數onPostExecute,在我的例子這將是一個Stringscore當你從doInBackground返回。

+0

對不起@Larry,但這似乎不工作正常! –

+0

您必須提供更多信息,瞭解您所做的更改以及您所看到的內容。 –

+0

感謝您的關注,但編輯後現在@Manishoaham的答案現在可以正常工作。我想你的答案的問題是省略'super.onPostExecute(finalScore)'方法。 –