2015-07-05 53 views
0

讓我先提供一些背景。我創建一個活動,在創建時,將調用ASyncTask來檢索膽固醇數據列表。這個ASyncTask是我的Activity中的一個私有類。一切正常,但是,我希望能夠在完成後將結果存儲回活動中。這樣一來,每次在活動中添加膽甾醇數據時,我只需要一次調用就可以追加數據,而不是重新列出整個事件。ASyncTask修改主線程上的變量

所以,我的問題是:我將如何設置onPostExecute修改MainActivity中的字段? 現在發生的事情是,它通過整個後臺任務,正確地填充臨時列表,返回Success,然後似乎永遠不會將它發送到onPostExecute(),我將臨時List複製到我的主活動列表中。

我粘貼下面的源代碼,我很抱歉,如果有點混亂。我從以前的活動中重用了一點。

public class CholestoralActivity extends ActionBarActivity { 
public GraphView graph; 
LineGraphSeries<DataPoint> series; 
public ArrayList<CholesterolInformation> cholesterolInformationList; 

public void setCholesterolInformationList(ArrayList<CholesterolInformation> cholesterolInformationList) { 
    this.cholesterolInformationList = cholesterolInformationList; 
} 

//TODO: Populate ArrayList given JSON response, and call generateDataPoints() 
private class ListCholestoralAPI extends AsyncTask<String, Void, String> { 
    private ArrayList<CholesterolInformation> tmpcholesterolInformationList; 

    @Override 
    protected String doInBackground(String... params){ 
     tmpcholesterolInformationList = new ArrayList<>(); 
     tmpcholesterolInformationList.clear(); 
     SharedPreferences sharedPreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE); 
     String email = sharedPreferences.getString("email", ""); 
     String listURL = params[0]; 
     listURL += email; 
     URL url = null; 
     String result = null; 
     try { 
      url = new URL(listURL); 
     } 
     catch (MalformedURLException e) { 
      return e.getMessage(); 
     } 
     try { 
      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
      httpURLConnection.setRequestMethod("GET"); 
      BufferedReader in = new BufferedReader(
        new InputStreamReader(httpURLConnection.getInputStream())); 
      String inputLine; 
      StringBuffer response = new StringBuffer(); 

      while ((inputLine = in.readLine()) != null) { 
        response.append(inputLine); 
      } 
      in.close(); 
      result = response.toString(); 
     } 
     catch (IOException e) { 
      return e.getMessage(); 
     } 
     try { 
      JSONObject listObject = new JSONObject(result); 
      JSONArray jsonArray = listObject.optJSONArray("cholesterol"); 
      if (jsonArray != null) { 
       for (int i=0; i < jsonArray.length(); i++){ 
        JSONObject jsonObject = jsonArray.optJSONObject(i); 
        CholesterolInformation cholesterolInformation = new CholesterolInformation(); 
        if (jsonObject != null) { 
         String HDL = jsonObject.getString("hdl"); 
         double hdl = Double.parseDouble(HDL); 
         String LDL = jsonObject.getString("ldl"); 
         double ldl = Double.parseDouble(LDL); 
         String triGlyceride = jsonObject.getString("triGlycaride"); 
         double tri = Double.parseDouble(triGlyceride); 
         String date = jsonObject.getString("date"); 
         String unit = jsonObject.getString("unit"); 
         cholesterolInformation.setDate(date); 
         cholesterolInformation.setHdl(hdl); 
         cholesterolInformation.setLdl(ldl); 
         cholesterolInformation.setTriGlycaride(tri); 
         cholesterolInformation.setUnit(unit); 
         tmpcholesterolInformationList.add(cholesterolInformation); 
        } 
       } 

      } 
     } 
     catch (Exception e){ 
      return e.getMessage(); 
     } 
     return "Success"; 
    } 

    protected void onPostExecute(String result) { 
     if ("Success".equals(result)) { 
      System.out.println("Successfully populated list."); 
      setCholesterolInformationList(tmpcholesterolInformationList); 
     } 
    } 
+0

你能記錄結果的值,並讓我們知道你會得到什麼嗎? –

+0

是的,我只是暫停了onPostExecute中的「if」語句的調試器,結果包含「成功」,臨時列表中有正確的信息。 –

+0

和'setCholesterolInformationList'方法被調用? –

回答

0

如果您的AsyncTask是在您的活動專用類,你可以在你存取權限變量Activity類

所以

protected void onPostExecute(String result) { 
     myTextViewWhichIsInActivity.setText(result); 
    } 

有您登錄 '結果' 嘗試

Log.i(TAG,"result is ["+result+"] is the same as Success"); 

這種方式可以確保你沒有任何尾隨空格或流氓錯別字,它總是比較好因爲它們比字符串更不容易出錯