如何填充一个ListView异步? [英] how to populate a listview asynchronously?

查看:154
本文介绍了如何填充一个ListView异步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我应该怎么实施 ListAdapter 的异步加载其观点成的ListView ?我想这样做,因为我填充列表从我的数据库,它使我的行为有点慢加载的时间的信息。

I am wondering how I should implement a ListAdapter that loads its views asynchronously into a ListView? I want to do this because I am populating the list with information from my database, which is making my activity a bit slow to load at times.

推荐答案

您可以使用的 的AsyncTask 和使用的 onPostExecute 方法来发布新加载的结果:

You can use an AsyncTask and use the onPostExecute methods to publish the newly loaded results:

private ArrayAdapter adapter = new YourArrayAdapter();

private class YourAsyncTask extends AsyncTask<Void, Void, List<YourItem>> {

    @Override
    protected void onPreExecute() {
        // start loading animation maybe?
        adapter.clear(); // clear "old" entries (optional)
    }

    @Override
    protected List<YourItem> doInBackground(Void... params) {
        // everything in here gets executed in a separate thread
        return DataBase.getItems();
    }

    @Override
    protected void onPostExecute(List<YourItem> items) {
        // stop the loading animation or something
        adapter.addAll(items);
    }
}

这篇关于如何填充一个ListView异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆