Search code examples
javaandroidservicebindbaseadapter

How to call my background service's public method from listview adapter


In my activity inside onStart() I bind into my custom service and create an instance of the service then I can call my service's public methods. Inside onStop() then I do unbindservice(myservice).

How can I do the same inside my listview adapter that extends BaseAdapter?

(There are public methods and variables I need to access inside onClickListener of my list items.)


Solution

  • If you succeed binding service with the activity then its simple to implement.

    Just pass the service instance to the adapter either in constructor or via method. Then use the service instance on the adapter's OnClick mehtod.

    MyService s;
    ...
    // Binding is done s is service instance then
    MyAdapter adapter = new MyAdapter(activityInstance, s, your data)
    

    You might have access to the service instance s inside the adapter.