android - 如何在同一快照侦听器(Firebase)的片段和活动(均独立)中显示数据
问题描述
想在我目前面临的奇怪问题上得到你的帮助。我尝试了几天但没有运气,最后决定在这里发帖寻求帮助。
我创建了一个附加到 Firebase 中的集合的快照侦听器,定义如下:-
public class FirebaseTypingStatusLiveData extends LiveData<List<documentSnapshot>> {
// Logging constant
private static final String TAG = "FirebaseQueryLiveData";
// Document Reference
private final DocumentReference documentReference;
// Listener
private final MyDocumentListener listener = new MyDocumentListener();
// Handler
private final Handler handler = new Handler();
private ListenerRegistration listenerRegistration;
// Flag to remove listener
private boolean listenerRemovePending = false;
private MutableLiveData <List<documentSnapshot> mutableLiveData = new MutableLiveData<>();
// Constructor
public FirebaseTypingStatusLiveData(DocumentReference documentReference) {
this.documentReference = documentReference;
}
public LiveData<List<documentSnapshot>> checknow(){
// Add listener
if (!Listeners.LIVESAMPLE.containsKey(documentReference)) {
listenerRegistration = documentReference.addSnapshotListener(listener);
Listeners.LIVESAMPLE.put(documentReference, listenerRegistration);
} else {
listenerRegistration = Listeners.LIVETYPINGSTATUSSAMPLE.get(documentReference);
}
return mutableLiveData;
}
// Listener definition
private class MyDocumentListener implements EventListener<DocumentSnapshot> {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable
FirebaseFirestoreException e) {
Log.d(TAG, "onEvent");
// Check for error
if (e != null) {
// Log
Log.d(TAG, "Can't listen to query snapshots: " + documentSnapshot
+ ":::" + e.getMessage());
return;
}
setValue(documentSnapshot);
mutableLiveData.setValue(documentSnapshot);
}
}
}
}
快照完美地读取数据,并在数据可用时提供建议。
快照数据正在显示 1. 在 Fragment 中(不是我所说的 Activity 的一部分) 2. Activity 通过两个具有相同代码的视图模型,如下所示:
@NonNull
public LiveData<List<documentSnapshot>> getDataSnapshotLiveData() {
Firestore_dB db = new Firestore_dB();
DocumentReference docref = db.get_document_firestore("Sample/"+docID);
FirebaseTypingStatusLiveData firebaseTypingStatusLiveData = new
FirebaseTypingStatusLiveData(docref);
return firebaseTypingStatusLiveData.checknow();
}
除了更改所有者之外,片段和活动代码也相同,如下所示:-
LiveData<List<documentSnapshot>> liveData = viewmodel.getDataSnapshotLiveData();
liveData.observe(this, new Observer<List<documentSnapshot>>() {
@Override
public void onChanged(DocumentReference docreef) {
String name = docreef.get("name");
stringname.setText(name); // The text is displaying either in Fragment or in Activity but not in both.
});
我的问题是我需要片段和活动中的数据,而我在片段或活动中获取数据取决于我评论的代码。
请告知我在哪里犯了错误。提前致谢
解决方案
我将 LiveData 设为静态,以侦听源数据的变化并提供不同活动中需要的更新内容。
推荐阅读
- elasticsearch - 使用 Fluentd 配置 Istio 会引发超时错误
- oracle - Kafka JDBC 接收器连接器 - Oracle
- php - Homebrew 升级丢弃 PHP:dyld:未加载库:/usr/local/opt/libpsl/lib/libpsl.5.dylib
- python - 从 DM 中的回复中获取消息内容
- node.js - 无法升级node js最新版本
- java - OnCreateViewHolder 性能问题
- javascript - 在 Leaflet 中传递给 DivIcon 的 HTMLElement 不起作用
- html - 隐藏离子菜单上的默认图标菜单汉堡按钮 | 离子 4
- python - TypeError: 'Add' Op 的输入'y' 的 float32 类型与参数'x' 的 int32 类型不匹配 tf.cast 没用
- sql-server - 如何以很少的变量在不同条件下分组的方式对表执行自连接