0

I am working on an application in which I have a background task executing via service. I have a list showing some data elements in it. My Scenario is:

1- User set something to execute in bg and does not close the app and leaves it in this state.

2- Background process starts working, it completes its work and finishes. But list is still showing the task user entered. It should have been removed from the list.

This is the very functionality I want to achieve. How can I achieve that?

Remember if user performs some action like navigate to other screen etc, I have coded it to get new list for view. But if user leaves it idle after scheduling something, view does not refresh. Please help me out of this. Any help is appreciated.

Community
  • 1
  • 1
Usama Sarwar
  • 8,922
  • 7
  • 54
  • 80

2 Answers2

0

call notifyDataSetChanged() on your list adapter.

notifyDataSetChanged() Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

Android. How does notifyDataSetChanged() method and ListViews work?. The accepted answer is the link has a good explanation.

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

I think you have to use Service with BroadcastReceiver to resolve your issue.

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

In Service you need to send

Intent intent = new Intent();
    // Bundle the counter value with Intent
    intent.putExtra("key", data);
sendBroadcast(intent); // finally broadcast

after your background operation done.

And finally

in your service calling Activity

private BroadcastReceiver myReceiver = new BroadcastReceiver() {

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

Hope this will work.

Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
Amit Gupta
  • 8,914
  • 1
  • 25
  • 33