2012-12-30 61 views
0

我想把我從一個JSON提要的內容放入ListView,使用Android異步Http客戶端處理HTTP請求。但是,我認爲請求的性質會導致適配器接收到值而不是我想要的數據數組。異步GET搞砸ListView

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ApplicationGlobal g = (ApplicationGlobal) getApplication(); 
    boolean userLoggedIn = g.getUserLoggedIn(); 
    AsyncHttpClient clientSession = new AsyncHttpClient(); 
    PersistentCookieStore cookieStore = g.getCookieStore(); 
    clientSession.setCookieStore(cookieStore); 
    if (!userLoggedIn) { 
     login(); 
    } else { 

     ArrayList<HashMap<String, String>> newsFeed = getNewsJson(); 




     // Second to be printed: 
     System.out.println("LIST DATA"); 



        System.out.println(newsFeed); 
     SimpleAdapter adapter = new SimpleAdapter(
       this, 
       newsFeed, 
       R.layout.assignment_list_row_view, 
       new String[] { "photo", "dateAssigned", "dateDue", 
         "description" }, 
       new int[] { R.id.text1, R.id.text2, R.id.text3, R.id.text4 }); 
     setListAdapter(adapter); 
    } 
} 

ArrayList<HashMap<String, String>> newsFeed = new ArrayList<HashMap<String, String>>(); 

private ArrayList<HashMap<String, String>> getNewsJson() { 
    ApplicationGlobal g = (ApplicationGlobal) getApplication(); 
    AsyncHttpClient clientSession = new AsyncHttpClient(); 
    PersistentCookieStore cookieStore = g.getCookieStore(); 
    clientSession.setCookieStore(cookieStore); 
    clientSession.get("http://192.168.1.42:5000/news/dummy/all/", 
      new JsonHttpResponseHandler() { 
       @Override 
       public void onSuccess(JSONObject response) { 
        JSONArray assignments = new JSONArray(); 
        try { 
         assignments = response.getJSONArray("assignments"); 
         JSONObject c = assignments.getJSONObject(0); 

         for (int i = 0; i < assignments.length() + 1; i++) { 

          JSONObject individualAssignment = new JSONObject(
            c.getString(Integer.toString(i))); 

          HashMap<String, String> map = new HashMap<String, String>(); 
          map.put("photo", 
            individualAssignment.getString("photo")); 
          map.put("dateAssigned", individualAssignment 
            .getString("date_assigned")); 
          map.put("dateDue", individualAssignment 
            .getString("date_due")); 
          map.put("description", individualAssignment 
            .getString("description")); 

          newsFeed.add(map); 


          // Last to be printed (and printed multiple times, as expected) 

          System.out.println("RAW DATA"); 
          System.out.println(newsFeed); 
         } 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 




    // First to be printed: 

    System.out.println("RETURNED DATA"); 
    System.out.println(newsFeed); 
    return newsFeed; 
} 

我認爲正在發生的事情,如上面的三個安置的意見,是onSuccess()不叫很快的傳遞到上用非空值的適配器的數據。

什麼是最好的方式去重新設計這段代碼正常工作?

回答

1

該方法將立即返回newsFeed,同時異步內容仍在執行。 將適配器的初始化放入onSuccess流中可能會解決您的問題。

我會推薦使用AsyncTask進行後臺操作,因爲它更清晰了什麼是在後臺執行,並知道什麼應該由主(UI)線程處理。

AsyncTask信息:http://developer.android.com/reference/android/os/AsyncTask.html

+0

非常感謝。 :) –