0

I have an app that contains a listview, button and textview. By clicking the button I add a new item to the ListView using ArrayAdapter. Now implemented a service that receives information from the server. How can I update Listview from my service, this is the event that is fired when my server sends me an answer:

    static void HandleOnRespose (object sender, ServerInEventArgs e)
    {
        //add in listview a reponse from server (e.CodeRespose)
    }
julius_cesars
  • 115
  • 14

1 Answers1

1

You have several ways to implement a service calling an activity (3 to be honest) you can read more about it here.
@zgc7009's suggestion is valid and probably good enough when dealing with trivial data types that can be put inside a Bundle, but can be a bit messy when dealing with more complex data, for example.
I think that using a callback is the most appropriate method (but it just might be a subjective thing).

To achieve calling an activity from a service with a callback

  1. You'd have to register the callback on the service. Depending on your implementation you could do something like so from the activity:

    yourService.someActionCallback = (codeResponse)=> 
     {
        //do whatever you want on your listview using the codeResponse
        lv.NotifyDataSetChanged();
     }
    
  2. Invoke your callback from the service:

    static void HandleOnRespose (object sender, ServerInEventArgs e)
    {
       callback(e.CodeRespose);
    }
    

That would be it.

P.S. you still have to make sure that the callback is not null and always register the callback (and "unregister" it on activity's destruction, but that's just basics.

Community
  • 1
  • 1
Alex.F
  • 5,648
  • 3
  • 39
  • 63