java - Is possible to use the instance ValueEventListener for two different calls with addValueEventListener, at same Activity?
问题描述
I'm using the method addValueEventListener as shown below:
public List<MyClassModel> DoRequestCards() {
valueEventListener = getDatabaseReference()
.child("Example")
.child(Objects.requireNonNull(getFirebaseAuthInstance().getCurrentUser()).getUid())
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
list.clear();
for (DataSnapshot data : dataSnapshot.getChildren()) {
MyClassModel model = data.getValue(MyClassModel.class);
//Retrieves UniqueKey from each example
Objects.requireNonNull(card).setUniqueId(data.getKey());
list.add(model);
}
taskListener.OnSuccess(); //call interface method
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
taskListener.OnError(databaseError.getMessage()); //call interface method
}
});
return list;
}
It's possible to create another method to retrieve different list with different data fetching again addValueEventListener
at same valueEventListener
instance ? Or should I create another instance to it (because they're at same activity and need each to stay listening their changes) ?
I'm using the same instance of ValueEventListener at different activities, but when activity stops (onStop) I remove it (I've no problems with that).
解决方案
您可以使用相同的侦听器两次。这不是问题。您可以在侦听器中检查哪个位置触发了侦听器的快照。
推荐阅读
- vue.js - 在没有警告消息的情况下无法获得 Vue.js 转换工作
- python - 如何将腌制的 sklearn GBM 分类器转换为 R GBM 对象?
- python - Python 2 子进程:无法从 readline 获取输出
- javascript - 使用 GAS 获取 URL,然后截屏(或将 HTML 转换为 PDF/JPG)
- javascript - 在带有 React Native 的标签栏中使用 FontAwesome
- python - numpy.load() 未加载
- performance - 如何在 .exe 应用程序中检查客户端的性能相关问题?
- java - 为什么这个程序打印“Hello”?(如何将函数指针转换为 Java 中的 Runnable.run())
- c++ - 无法去功能实现vim
- r - 如何在 R 中为多个绘图创建多个 pdf?