2 xiaoyan 12 xiaoyan_12 于 2013.07.22 13:36 提问

使用 async task 如何返回对象?

我想从 async task 里解析后返回 tweets 的列表,但是我从 task 中没有返回 arraylist。谁能提供一个解决方案啊?

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList listItems = new ArrayList();
        new myAsyncTask().execute(listItems);

        setListAdapter(new ArrayAdapter(this, R.layout.tweet, R.id.tweet,listItems));
        ListView lv = getListView();
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // When clicked, show a Toaster
                Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private class myAsyncTask extends AsyncTask<ArrayList<Object>, Void, Void>
    {
        ArrayList<Object> listItems;
        ProgressDialog dialog;
        @Override
        protected void onPreExecute() {
            dialog = ProgressDialog.show(Main.this, "", "Loading....");
        }
        @Override
        protected Void doInBackground(ArrayList<Object>... params) {

            String host = "api.twitter.com";
            String twitterURL = "http://"+host+"/1/statuses/user_timeline.json?screen_name=i1990jain&amp;count;=10";
            try {
                HttpClient client = new DefaultHttpClient();
                BasicHttpContext localContext = new BasicHttpContext();
                HttpHost targetHost = new HttpHost(host, 80, "http");
                HttpGet httpget = new HttpGet(twitterURL);
                httpget.setHeader("Content-Type", "application/json");
                HttpResponse response = client.execute(targetHost, httpget, localContext);
                HttpEntity entity = response.getEntity();
                Object content = EntityUtils.toString(entity);
                Log.d(MY_APP_TAG, "OK: " + content.toString());

                JSONArray ja = new JSONArray(content.toString());

                for(int i = 0; i < ja.length(); i++){
                    JSONObject jo = ja.getJSONObject(i);
                    listItems.add(jo.getString("text"));
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            dialog.dismiss();
        }
    }
}

2个回答

Jave_
Jave_   2013.07.22 13:46

把doInBackground中的最后一句return null改为return listItems呢

shaojie519
shaojie519   2013.07.22 16:26

private class myAsyncTask extends AsyncTask, Void, ArrayList >

....
@Override
protected ArrayList doInBackground(ArrayList... params) {

........

return listItem;
}
@Override
protected void onPostExecute(ArrayList result) {
dialog.dismiss();
setListAdapter(new ArrayAdapter(XXXactivity.this, R.layout.tweet, R.id.tweet,result));
}

Csdn user default icon
上传中...
上传图片
插入图片