我有一个Service
我的应用程序中的类,我必须使用 FirebaseListAdapter<String>
就在其中。
这是MyService.java
文件:
public class MyService extends Service {
DatabaseReference firebaseDatabaseRef;
String followedBy;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
firebaseDatabaseRef = FirebaseDatabase.getInstance().getReferenceFromUrl("https://***-***.firebaseio.com/");
Toast.makeText(getBaseContext(), "Service started", Toast.LENGTH_SHORT).show();
FirebaseListAdapter<String> adapter = new FirebaseListAdapter<String>(this,
String.class, android.R.layout.simple_expandable_list_item_1, firebaseDatabaseRef) {
@Override
protected void populateView(View v, String model, int position) {
followedBy = model.substring(30);
}
};
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(getBaseContext(), "Service stopped", Toast.LENGTH_SHORT).show();
}
}
但无法编译,因为我收到此错误:
Error:(133, 47) error: no suitable constructor found for FirebaseListAdapter(MyService,Class<String>,int,DatabaseReference)
constructor FirebaseListAdapter.FirebaseListAdapter(Activity,Class<String>,int,Query) is not applicable
(argument mismatch; MyService cannot be converted to Activity)
constructor FirebaseListAdapter.FirebaseListAdapter(Activity,Class<String>,int,DatabaseReference) is not applicable
(argument mismatch; MyService cannot be converted to Activity)
我应该用什么来代替 this
在 FirebaseListAdapter<String>
成功编译并运行我的代码?
请告诉我。
最佳答案
Activity 是提供用户界面的应用程序组件。 Service 的文档解释:
A Service is an application component that can perform long-running operations in the background, and it does not provide a user interface
FirebaseListAdapter支持显示 Firebase 数据库中的数据。它在 Activity
中使用。它不能在服务
中使用。无需在 Service
中使用它,因为 Services 没有用户界面(除了可选的通知之外)。
关于java - 如何在服务中使用 FirebaseListAdapter<String>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41082501/