简体   繁体   English

如何从listview适配器调用我的后台服务的公共方法

[英]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. 在我的onStart()里面的活动中,我绑定到我的自定义服务并创建一个服务实例然后我可以调用我的服务的公共方法。 Inside onStop() then I do unbindservice(myservice). 在onStop()里面然后我做unbindservice(myservice)。

How can I do the same inside my listview adapter that extends BaseAdapter? 如何在扩展BaseAdapter的listview适配器中执行相同的操作?

(There are public methods and variables I need to access inside onClickListener of my list items.) (我需要在列表项的onClickListener中访问公共方法和变量。)

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. 然后在适配器的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. 您可能必须在适配器内部接入服务实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM