這是我的問題。我創建了一個asyncTask鏈接到我的數據庫並使用JSON發送和接收信息。AsyncTask - 如何在onPostExecute之後將值傳遞迴活動類?
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
returnValues = dataParsed.split("\\s+");
mainActivity.getValue(this is the function that calls anotherfunction in
asyncTask)
Log.v("ARRAY LENGTH", String.valueOf(returnValues.length));
}
public String[] returnmyString(){
//return mySaveData;
Log.v("ARRAY LENGTH 2", String.valueOf(returnValues.length));
return returnValues;
}
我在基於活動的類中創建asyncTask對象,然後調用該object.execute。我的問題是,我的代碼將繼續運行,一旦調用object.execute並且其中一行在執行完所有代碼之前調用asyncTask類中的函數。
process.activitySave(1); //<---Process is the object for the asyncTask class
process.ContextSave(this,ServerURLSource,myParameters);
process.execute()
changedData = process.returnmyString(); //<-- this is the line of code that gets implemented that returns a null value
我試圖創建在的AsyncTask類Mainactivity對象,然後調用一個函數,然後當我這樣做檢索的價值,但我的應用程序崩潰。任何幫助,將不勝感激。我想在mainactivity類中添加某種監聽器,因爲它似乎無法在我的asyncTask類中引用mainactivity類中的任何函數。
這是的AsyncTask內的函數返回值:
public String[] returnmyString(){
//return mySaveData;
Log.v("ARRAY LENGTH", String.valueOf(returnValues.length));
return returnValues;
}
你需要一個回調方法。當任務x完成時,將結果傳遞給活動,方法是將反饋作爲參數 – Zoe
嘿,你可以進一步解釋這種類型的功能嗎?任務x將是process.execute,但是我會使用哪種回調方法,以及我將它放在哪裏?那麼我會用什麼樣的方法來獲得反饋? – RW6
你爲什麼不簡單地將'changedData'設置到'onPostExecute' @ RW6? onPostExecute方法在您調用#execute方法的相同線程上運行。 – tolpp