- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我已经使用 FirebaseUI-Android 库 实现了 RecyclerView
。
一旦我使用 FirebaseRecyclerAdapter
RecyclerView
实时数据就会发生变化
在 Collection 中,数据文档的字段类型为 Boolean , Integer, Reference .
我想使用该引用通过 addSnapshotListener
在 populateViewHolder
中获取数据。
帮帮我!这是我的代码:
FirebaseRecyclerAdapter<Conv, ConvViewHolder> firebaseConvAdapter = new FirebaseRecyclerAdapter<Conv, ConvViewHolder>(
Conv.class,
R.layout.users_single_layout,
ConvViewHolder.class,
conversationQuery
) {
@Override
protected void populateViewHolder(final ConvViewHolder convViewHolder, final Conv conv, int i) {
final String list_user_id = getRef(i).getKey();
final DocumentReference docRef = db.collection("cities").document(list_user_id);
docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot snapshot,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.w(TAG, "Listen failed.", e);
return;
}
if (snapshot != null && snapshot.exists()) {
Log.d(TAG, "Current data: " + snapshot.getData());
} else {
Log.d(TAG, "Current data: null");
}
}
});
}
};
mConvList.setAdapter(firebaseConvAdapter);
Firebase 表示,如果您添加 addSnapshotListener,则必须在不需要时将其删除 Detach a listener
When you are no longer interested in listening to your data, you must detach your listener so that your event callbacks stop getting called. This allows the client to stop using bandwidth to receive updates. You can use the unsubscribe function on
onSnapshot()
to stop listening to updates.
最佳答案
为此,您需要使用 EventListener<DocumentSnapshot>
像这样:
EventListener<DocumentSnapshot> eventListener = new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(DocumentSnapshot snapshot, FirebaseFirestoreException e) {
if (snapshot != null && snapshot.exists()) {
//Do what you need to do
}
}
};
声明一个全局的 ListenerRegistration listenerRegistration;
变量并在需要的地方添加 SnapshotListener,如下所示:
if (listenerRegistration == null ) {
listenerRegistration = yourRef.addSnapshotListener(eventListener);
}
要删除监听器,只需使用以下代码行,在您的 onStop()
中方法:
@Override
protected void onStop() {
if (listenerRegistration != null) {
listenerRegistration.remove();
}
}
此外,不要忘记在您的 onStart()
之后再次添加它。方法被调用。
@Override
protected void onStart() {
super.onStart();
listenerRegistration = yourRef.addSnapshotListener(eventListener);
}
当您调用 addSnapshotListener
时对于listening to realtime updates ,这意味着您附加了一个监听器,该监听器会在数据库中发生的每个更改时被调用。所以当你的应用程序关闭时也会发生这种情况,这就是为什么它是 detach the listeners 的强制性原因。在 Activity 被销毁之前。
如果在您的应用中您不需要实时获取数据,那么您可以简单地使用 get()直接调用引用,它只读取文档一次。由于它只读取一次,因此没有要删除的监听器。我是addListenerForSingleValueEvent()
的通讯员用于 Firebase 实时数据库。
还有一种更优雅的移除监听器的方法,即首先将 Activity 作为 addSnapshotListener() 中的参数传递。方法,因此 Firestore 可以在 Activity 停止时自动清理监听器。
ListenerRegistration lg = yourDocumentRef
.addSnapshotListener(YourActivity.this, eventListener);
关于java - 如何在 RecyclerView Item 的 populateViewHolder 中设置 addSnapshotListener 和 remove?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48699032/
FirebaseRecyclerAdapter - populateViewHolder 第一次运行时没有填充数据,但是当我关闭应用程序并打开它时,数据绑定(bind)在 RecyclerView 中
我正在使用 FirebaseRecyclerAdapter 从 Firebase 获取数据并填充回收器 View 。代码工作正常,但是当我开始滚动时,回收器 View 会在每次滚动时调用 Popula
我试图将我的实时数据库中的一些数据显示到回收器适配器中,但没有成功。我已经让 FirebaseRecyclerAdapter 在另一个 Activity (java) 上工作,但在这个 Activit
我尝试从 fragment onViewCreated 方法中的 database 检索数据 我使用了如下所示的 FirebaseRecylerAdapter @Override public
我有一个如下所示的 Firebase 数据结构。我正在尝试使用 FirebaseRecyclerAdapter 加载此数据。问题是 populateViewHolder() 没有被调用。对数据库结构的
我的 firebase 控制台中有以下结构。我正在尝试读取值并显示所有用户,但从未调用 populateViewHolder。 users +OW5BYennVRXvfzOjfKpup
我正在尝试使用 FirebaseRecyclerAdapter 使用我的 Firebase 数据库中的数据在 fragment 中填充 RecyclerView。 我的主要问题是 populateVi
我想在我的项目中使用 FirebaseRecyclerAdapter 并且我在之前的项目中使用了这段代码 FirebaseRecyclerAdapter adapter = new FirebaseR
我想从 firebase 检索数据。当我添加 FirebaseRecyclerAdapter 方法时 populateviewholder 方法没有实现而不是这个。另外两个onCreateViewHo
我已经使用 FirebaseUI-Android 库 实现了 RecyclerView。 一旦我使用 FirebaseRecyclerAdapter,我的 RecyclerView 实时数据就会发生变
我是一名优秀的程序员,十分优秀!