【问题标题】:How to insert data from DataSnapshot to LiveData<List<Model>>?如何将数据从 DataSnapshot 插入 LiveData<List<Model>>?
【发布时间】:2021-04-06 19:52:30
【问题描述】:

我需要将从 firebase 获得的数据插入到 LiveData,我不知道该怎么做

public LiveData<List<MedicationViewModel>> getAllMeds(){

        mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
             LiveData<List<MedicationViewModel>> medics; //Return this 
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for(DataSnapshot ds : dataSnapshot.getChildren()) {

                            MedicationViewModel medic = ds.getValue(MedicationViewModel.class);
                            Log.d("TAG", medic.getMedName());
                             //TODO
                        }

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
    }

【问题讨论】:

  • 这是一个有用的资源,它详细讨论了这件事。firebase.googleblog.com/2017/12/…
  • 我无法理解您的代码。什么 id MedicationViewModel 以便我知道可以提供帮助。因为,您不能拥有继承自ViewModel 或AndroidViewModel 的对象列表。它不是那样工作的

标签: android firebase firebase-realtime-database android-livedata lifecycle


【解决方案1】:

你应该把你的问题分成几个步骤:

  1. 返回 livedata:如果你想要一个 livedata,这意味着你希望有人观察它,所以我建议只拥有一个 livedata 并将它返回给任何想要观察的人

    //=======Observer Activity/Fragment=============:
    final Observer<List<MedicationViewModel>> medsObserver = new Observer<List<MedicationViewModel>>() {
        @Override
        public void onChanged(@Nullable final List<MedicationViewModel> medsList) {
            // Do what you need when the new list arrives
        }
    };
    yourObject.getAllMeds().observe(context, medsObserver) //setting the observer
    //===============Your class============
    private final MutableLiveData<List<MedicationViewModel>> allMedsLiveData = new MutableLiveData<>();
    //...
    public LiveData<List<MedicationViewModel>> getAllMeds(){
       return allMedsLiveData; //returning the liveData
    }
    
  2. 来自firebase时填写数据:

    //===============Your class============
    private final MutableLiveData<List<MedicationViewModel>> allMedsLiveData = new MutableLiveData<>(); //the same one as the step 1
    private final List<MedicationViewModel> allMedsList = new ArrayList<>();
    //...
    private void setUpListener() {
       mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for(DataSnapshot ds : dataSnapshot.getChildren()) {
    
                        MedicationViewModel medic = ds.getValue(MedicationViewModel.class);
                        Log.d("TAG", medic.getMedName());
                        allMedsList.add(medic); // or update if exists (just adding to keep things simple, but you get the idea)
                    }
                    allMedsLiveData.postValue(allMedsList) //Using postvalue here in case the DB is fetching data on a worker thread, as it should. This call will launch the "medsObserver" observer on the observer activity/fragment on step 1
    
                }
    
                @Override
                public void onCancelled(DatabaseError databaseError) {
    
                }
            });
     }
    

希望它对你有用!

【讨论】:

    猜你喜欢
    • 2020-01-27
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多