通过异步任务更新列表视图

时间:2014-12-30 11:17:54

标签: java android asynchronous android-asynctask

我正在尝试创建一个通过get请求检索数据的应用程序,并将其显示在列表视图中。请求通过异步任务完成。但是,异步任务无法更新列表。

public class MainActivity extends Activity {
    TextView getResponse;
    ArticleTeaserAdapter articleTeaserAdapter;
    private ArrayList<ArticleTeaser> articleTeasers = new ArrayList<ArticleTeaser>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getResponse = (TextView) findViewById(R.id.textView1);
        ListView listView = (ListView) findViewById(R.id.article_teasers_list);
        articleTeaserAdapter = new ArticleTeaserAdapter();
        new HttpAsyncTask().execute("http://content.url.com/search?api-key=test&page-size=5&order-by=newest");
    }

    private class HttpAsyncTask extends AsyncTask<String, Void, String> {
        protected String doInBackground(String... urls) {
            return GET(urls[0]);
        }

        protected void onPostExecute(String result) {
            String response = "";
            JSONObject jObject;
            try {
                jObject = new JSONObject(result);
                JSONObject jObject2 = jObject.getJSONObject("response");
                JSONArray jArray = jObject2.getJSONArray("results");
                for (int i = 0; i < jArray.length(); i++) {
                    Map<String,String> out = new HashMap<String, String>();
                    out = parse((JSONObject)jArray.get(i),out);
                    Iterator it = out .entrySet().iterator();
                    while (it.hasNext()) {
                        Map.Entry pairs = (Map.Entry)it.next();
                        if (pairs.getKey().equals("webTitle")) {

                            articleTeasers.add(new ArticleTeaser(pairs.getValue().toString()));
                        }
                        it.remove(); // avoids a ConcurrentModificationException
                    }
                }
                articleTeaserAdapter.notifyDataSetChanged();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
   }

    public class ArticleTeaserAdapter extends BaseAdapter {
        public ArticleTeaserAdapter() {
            /*articleTeasers.add(new ArticleTeaser("Local news 2"));
            articleTeasers.add(new ArticleTeaser("Local news 3"));
            articleTeasers.add(new ArticleTeaser("Local news 4"));
            articleTeasers.add(new ArticleTeaser("Local news 5"));*/
        }
        @Override
        public int getCount() {
            return articleTeasers.size();
        }

        @Override
        public Object getItem(int arg0) {
            return articleTeasers.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            return arg0;
        }

        @Override
        public View getView(int index, View view, ViewGroup parent) {
            if(view == null) {
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                view = inflater.inflate(R.layout.article_teaser_list_item, parent, false);
            }
            ArticleTeaser articleTeaser = articleTeasers.get(index);
            TextView articleTeaserTextView = (TextView) view.findViewById(R.id.title_view);
            articleTeaserTextView.setText(articleTeaser.getTitle());
            return view;
        }
    }
}

当我尝试通过适配器的构造函数添加数据时,一切正常。问题是当我使用asynctask

时listview不会更新

1 个答案:

答案 0 :(得分:0)

onPostExecute中创建新适配器的实例并在其中添加数据,并仅在列表不为空时更新适配器,

           if (articleTeaserAdapter == null) {
                articleTeaserAdapter = new ArticleTeaserAdapter();
                setListAdapter(articleTeaserAdapter);
            } else {
                articleTeaserAdapter.notifyDataSetChanged();
            }