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