首页 > 解决方案 > 如何在同一快照侦听器(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.
      });

我的问题是我需要片段和活动中的数据,而我在片段或活动中获取数据取决于我评论的代码。

请告知我在哪里犯了错误。提前致谢

标签: androidfirebaseandroid-fragmentsfirebase-storageandroid-livedata

解决方案


我将 LiveData 设为静态,以侦听源数据的变化并提供不同活动中需要的更新内容。


推荐阅读