java - 如何在服务中使用 FirebaseListAdapter<String>?

标签 java android firebase service compiler-errors

我有一个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)

我应该用什么来代替 thisFirebaseListAdapter<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/

相关文章:

java - 如何知道 firebase ChildEventListener 是否找到了 child ? [安卓]

java - 在Java中将集合的集合转换为列表的列表

java - 在 Java8 中连接对象数组列表

java - 无法启动 Activity ComponentInfo : android. view.InflateException: Binary XML file line #63: Error inflating class <unknown>

php - 在没有互联网连接的情况下注册 Android 应用程序

android - CWAC 相机 - 不确定使用 setCameraView()

firebase - 无法弄清楚如何使用 Gridsome-Plugin-Firestore

javascript - 如何使用 rxjs 返回结果列表

java - 包含 Spring Boot/Thymeleaf 应用程序的模板

java - for 循环外部变量的动态添加和使用 - Selenium