问题
I have a Fragment with a TextView that opens a custom DialogFragment with a RecyclerView list in it. I am using an MVVM structure with a ViewModel, Dao, and Repository to populate the RecyclerView list with a POJO item (Currency).
Whenever I install or restart the app, there is a delay in the RecyclerView list being populated. It initially shows the DialogFragment with no data in it and from using the debugger, it seems that it only populates once the onChanged method is called for my Observer on my LiveData>. I've added a GIF below that shows the delay.
Delayed DialogFragment RecyclerView Data
Is there a way to have this initially populate before the onChanged method? Or to remove this delay? I've added my code that generates this DialogFragment.
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Log.d(TAG, "onCreateDialog: Creating Dialog, getActivity == " + getActivity());
// Creates an Alert Dialog Builder item to help create the Currency Picker Fragment
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = requireActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.fragment_currency_picker, null);
RecyclerView currencyRecycler = view.findViewById(R.id.currency_picker_recycler);
currencyRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
currencyRecycler.setHasFixedSize(true);
CurrencyPickerViewModel currencyPickerViewModel = new ViewModelProvider(this).get(CurrencyPickerViewModel.class);
mAdapter = new CurrencyListRecyclerAdapter( this);
mAdapter.setCurrencies(currencyPickerViewModel.getAllCurrencies().getValue());
currencyRecycler.setAdapter(mAdapter);
currencyPickerViewModel.getAllCurrencies().observe(this, new Observer<List<Currency>>() {
@Override
public void onChanged(@Nullable List<Currency> currencies) {
// Triggered when there is a change made in the CurrencyPickerViewModel's LiveData mCurrencyList (ie. from the database)
Log.d(TAG, "onChanged: Observed a change, notifying adapter");
Log.d(TAG, "onChanged: size of currencyList = " + currencies.size());
mAdapter.setCurrencies(currencies);
}
});
builder.setView(view).
setTitle("Account Currency")
.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int id) {
Log.d(TAG, "onClick: Done Clicked");
mCurrencyPickerListener.applyCurrency(mSelectedCurrency);
}
})
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int id) {
Log.d(TAG, "onClick: Cancel Clicked");
CurrencyPickerFragment.this.getDialog().cancel(); // Closes the dialog
}
});
// Returns the created AlertDialog
return builder.create();
}
Also, I think it may have something to do with the prepopulation of my database since it's an asynchronous task but I don't know enough about how it works to see.
// Class for the initial population of the Currency database
private static class PopulateDbAsyncTask extends AsyncTask<Void, Void, Void> {
private CurrencyDao currencyDao;
public PopulateDbAsyncTask(CurrencyDatabase db) {
currencyDao = db.currencyDao();
}
@Override
protected Void doInBackground(Void... voids) {
currencyDao.insert(new Currency("Albanian Lek", "L"));
currencyDao.insert(new Currency("Australian Dollar", "$"));
currencyDao.insert(new Currency("British Pound", "P"));
currencyDao.insert(new Currency("Canadian Dollar", "$"));
currencyDao.insert(new Currency("Chinese Yuan", "Y"));
currencyDao.insert(new Currency("Danish Krone", "K"));
currencyDao.insert(new Currency("Egyptian Pound", "P"));
currencyDao.insert(new Currency("Euro", "€"));
currencyDao.insert(new Currency("Japanese Yen", "YY"));
currencyDao.insert(new Currency("Russian Ruble", "R"));
currencyDao.insert(new Currency("Swiss Franc", "F"));
currencyDao.insert(new Currency("UK Pound", "£"));
currencyDao.insert(new Currency("United States Dollar", "$"));
Log.d(TAG, "doInBackground: Created list of all currencies");
return null;
}
}
来源:https://stackoverflow.com/questions/60498005/livedata-getvalue-returns-null-values-before-onchanged-method