简体   繁体   English

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

[英]How to use FirebaseListAdapter<String> in a Service?

I have a Service class in my app and I have to use FirebaseListAdapter<String> in it. 我的应用程序中有一个Service类,必须在其中使用FirebaseListAdapter<String>

Here's MyService.java file: 这是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();
    }
}

but it can't be compiled as I'm getting this error: 但由于出现此错误而无法编译:

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)

What am I supposed to put in place of this in the FirebaseListAdapter<String> to compile and run my code successfully? 我应该在FirebaseListAdapter<String>中代替this来成功编译和运行我的代码吗?

Please let me know. 请告诉我。

Activities are the app components that provide the user interface. 活动是提供用户界面的应用程序组件。 The documentation for Service explains: 服务文档说明:

A Service is an application component that can perform long-running operations in the background, and it does not provide a user interface 服务是可以在后台执行长时间运行的操作的应用程序组件,并且不提供用户界面

FirebaseListAdapter supports the display of data from a Firebase database. FirebaseListAdapter支持显示Firebase数据库中的数据。 It is used in an Activity . 用于Activity It cannot be used in a Service . 不能在Service There is no need to use it in a Service , because Services have no user interface (other than an optional Notification). 无需在Service使用它,因为Services没有用户界面(可选的Notification除外)。

Please provide your Firebase initialization and Query ref. 请提供您的Firebase初始化和查询参考。

Are you using FirebaseListAdapter inside a fragment ?. 您是否在片段中使用FirebaseListAdapter?

this refers to the context of the activity. this是指活动的上下文。 If you are having the FirebaseListAdapter inside Fragment, then use getActivity() instead of this 如果您在Fragment中包含FirebaseListAdapter ,请使用getActivity()代替this

Please check the below code I have used. 请检查以下我已使用的代码。 In this, I have initialized the FirebaseListAdapter in Activity. 在此,我已经在Activity中初始化了FirebaseListAdapter。

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users");
Query qRef = ref.orderByChild("username");
FirebaseListAdapter<User> firebaseAdapter = new FirebaseListAdapter<User>(this,User.class,R.layout.list_item,qRef) {
            @Override
            protected void populateView(View v, User model, int position) {
                Log.i("MainAct","Populating View");
                TextView textView = (TextView) v.findViewById(R.id.list_item_text);
                textView.setText(model.getUsername());

            }
        };

It looks like the FirebaseRecyclerAdapter doesn't require a context to initialize, so you should be able to do: 看起来FirebaseRecyclerAdapter不需要上下文即可初始化,因此您应该能够:

FirebaseListAdapter<String> adapter = new FirebaseListAdapter<String>(
            String.class, android.R.layout.simple_expandable_list_item_1, firebaseDatabaseRef) {
   ...

See the code . 参见代码

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

相关问题 如何在2种布局中使用FirebaseListAdapter - How to use FirebaseListAdapter with 2 layouts 使用Android FirebaseListAdapter时如何使用两种布局? - How can I use two layouts when using Android FirebaseListAdapter? 我们可以使用 FirebaseListAdapter 在 android 中填充 TabLayout 吗? - Can we use FirebaseListAdapter to populate TabLayout in android? 如何获取 FirebaseListAdapter 中存在的元素数量? - How to get count of elements present in FirebaseListAdapter? 如何在Web服务中使用JAXBElement <String>? - How to use JAXBElement<String> in Web Service? FirebaseListAdapter无法将java.lang.String类型的对象转换为foo.bar.ChatMessage类型 - FirebaseListAdapter Can't convert object of type java.lang.String to type foo.bar.ChatMessage Android将String形式的Service从Service传递到Service?并在Service中的onCreate上使用? - Android Pass String form Service to Service?And use at onCreate in Service? Android - FirebaseListAdapter 以相反的顺序? - Android - FirebaseListAdapter in reverse order? FirebaseListAdapter 没有 populateView 没有被调用 - FirebaseListAdapter not populateView not being called 如何在方法中使用字符串在Android Application Java中为Web服务创建动态参数 - How to use string in method to create a dynamic parameter for Web Service in Android Application Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM