4

我试图从异步任务解析后返回推文列表....但我没有从任务中取回数组列表。任何人都可以提出解决方案吗?

public class Main extends ListActivity {
    String MY_APP_TAG = "com.list";

    /** Called when the activity is first created. */
    @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();
        }
    }
}
4

4 回答 4

3

AsyncTask<Params, Progress, Result>。所以你应该将它声明为AsyncTask<Void, Void, ArrayList<Object>>.

doInBackground从方法中返回列表。

于 2012-11-04T21:36:19.060 回答
1

要使用来自异步线程的新数据更新您的活动,您可以使用 onProgressUpdate()/onPostExecute() 方法,或者如果您使用的是 Thead,您应该使用 Heandler。

//aciticty class:
final Handler mHandler = new Handler();

//inside an async thread
handler.post(new Runnable() {
    @Override
    public void run() {
        //update GUI thread here
    }
});
于 2012-11-04T21:41:15.170 回答
1

你想在哪里返回你的结果?您可以在以下位置访问 listItems:

protected void onPostExecute(Void result) {

这就是您应该使用 listItems 来填充您的列表的地方。此方法在 UI 线程上运行,因此在其中执行它是安全的。

在您的 doInBackground 中,我没有看到 listItems 被初始化为任何值,因此它可能为空。您实际上应该创建单独的 listItems 实例以在 doInBackground 中使用,然后将其分配给 onPostExecute 内的 listView。

于 2012-11-04T21:42:27.357 回答
1

我是如何让它工作的

public class Main extends ListActivity {

    String MY_APP_TAG = "com.vidyut";
    ArrayList<Object> list = new ArrayList<Object>();
        /** Called when the activity is first created. */
    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myAsyncTask task = new myAsyncTask();
        task.execute();

}

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

        @Override
        protected ArrayList<Object> 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);
           list.add(jo.getString("text"));
          }
         } catch(Exception e) {
          e.printStackTrace();
         }
            return list;
        }
        protected void onPostExecute(ArrayList<Object> list) {
            dialog.dismiss();

             Log.d(MY_APP_TAG, "The returned list contains " +list.size()+ "elements");
             setListAdapter(new ArrayAdapter<Object>(Main.this, R.layout.tweet, R.id.tweet,list));
                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();
             }
            });

        }

    }
}
于 2012-11-05T21:13:20.250 回答