如何刷新ListView当服务完成其工作?

时间:2013-03-13 12:18:10

标签: android android-listview android-service android-arrayadapter

我正在开发一个应用程序,我通过服务执行后台任务。我有一个列表,显示其中的一些数据元素。我的情景是:

  

1-用户设置要在bg中执行的内容,并且不会关闭应用程序并使其处于此状态。

     

2-后台流程开始工作,完成工作并完成。但列表仍显示用户输入的任务。它应该已从列表中删除。

这是我想要实现的功能。我怎样才能做到这一点?

请记住如果用户执行某些操作,例如导航到其他屏幕等,我已对其进行编码以获取新的视图列表。但是如果用户在安排某些事情后让它空闲,则视图不会刷新。请帮我解决这个问题。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

在列表适配器上调用notifyDataSetChanged()。

notifyDataSetChanged()通知附加的观察者基础数据已被更改,反映数据集的任何视图都应自行刷新。

Android. How does notifyDataSetChanged() method and ListViews work?。接受的答案是链接有一个很好的解释。

答案 1 :(得分:0)

我认为您必须使用Service with BroadcastReceiver来解决您的问题。

// Register Broadcast Receiver
        IntentFilter filter = new IntentFilter(MyService.MYOWNACTIONFILTER);
        registerReceiver(myReceiver, filter);

在服务中您需要发送

Intent intent = new Intent();
    // Bundle the counter value with Intent
    intent.putExtra("key", data);
sendBroadcast(intent); // finally broadcast
完成后台操作后

最后

在您的服务中调用活动

private BroadcastReceiver myReceiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {
           // do your operation and refresh the list
        }
   }

希望这会奏效。

相关问题