异步GET搞砸了ListView

时间:2012-12-30 02:10:03

标签: android asynchronous android-asynctask

我正在尝试将来自JSON Feed的内容放入ListView,使用Android异步Http客户端处理HTTP请求。但是,我认为请求的性质导致适配器接收null值而不是我想要的数据数组。

@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 个答案:

答案 0 :(得分:1)

该方法将立即返回newsFeed,同时异步内容仍在执行。 将适配器的初始化放在onSuccess流中可能会解决您的问题。

我建议使用AsyncTask进行后台操作,因为在后台执行的内容更加清晰,并且知道主(UI)线程应该处理什么。

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