【问题标题】:I am looking to refresh a ListView without reloading the page我希望在不重新加载页面的情况下刷新 ListView
【发布时间】:2012-02-18 02:12:35
【问题描述】:

我希望在不重新加载页面的情况下刷新ListView。更准确地说,我有一个服务在Activity 中为ListView 发送数据,但是ActivityService 获取数据之前很久就加载了。所以我需要能够在Activity 已经加载之后加载/重新加载ListView

【问题讨论】:

    标签: android android-layout android-intent android-widget


    【解决方案1】:

    我发现了notifyDataSetChanged only works if you use the add, insert, remove, and clear functions on the Adapter,所以我最终在类似的实现中采用了以下方式:

    AsyncTask 获取doInBackground 中的所有数据。然后,完成后,我第一次在onPostExecute 中设置了列表适配器。为了让用户知道正在加载某些内容,我只是在Listview 顶部放置了一个TextView 并将其文本设置为onPreExecute 中的“正在加载..”,然后在数据时使其在onPostExecute 中不可见准备好了。

    如果需要刷新数据,只需再次执行AsyncTask即可。

    我喜欢这种方式,因为您只设置一次ArrayAdapter(即当您最终拥有所有数据时)。 Here 更多关于 AsyncTask 以备您需要时使用。文档有一些很好的示例代码。

    【讨论】:

      【解决方案2】:

      只要您想刷新它,请在ListViewAdapter 上致电notifyDataSetChanged()

      【讨论】:

        【解决方案3】:

        我会说使用IntentService 而不是Service。通过使用IntentService,您将能够将数据发送到后台Service,并在触发BroadCastReceiver 以更新您的UI 时接收更新的数据。 Here 是一个完整的示例,您可以使用 IntentService 来完成您的任务。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-20
          • 2017-08-13
          • 2013-10-09
          • 2011-10-22
          • 1970-01-01
          相关资源
          最近更新 更多