如何从DataSnapshot插入数据到LiveData<List<Model>>?
How to insert data from DataSnapshot to LiveData<List<Model>>?
我需要将从 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) {
}
});
}
您应该将您的问题分成几个步骤:
Return 实时数据:如果你想要一个实时数据,那就意味着你想要有人观察它,所以我建议只拥有一个实时数据和 return 它到谁要这样观察
//=======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
}
来自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) {
}
});
}
希望对你有用!
我需要将从 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) {
}
});
}
您应该将您的问题分成几个步骤:
Return 实时数据:如果你想要一个实时数据,那就意味着你想要有人观察它,所以我建议只拥有一个实时数据和 return 它到谁要这样观察
//=======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 }
来自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) { } }); }
希望对你有用!