I have already referred Retrieving data from Firebase Realtime Database in Android. My question is an extension to this. I am trying to use the data retrieved from firebase for AutoCompleteTextView through an ArrayAdapter. But both ArrayList & Adapter are "null" outside the onDataChange method. Guessing it is due to delay in retrieving the data from the database (Adapter is set even before the completion of data load). Is there a callback method to wait for the data load or what is the best way to handle this? Also, tried with notifyDataSetChanged on Adapter inside the onDataChange method, still no luck! I am dynamically adding AutoCompleteTextView's so I can't set the adapter to AutoCompleteTextView inside onDataChange method.
my code...
Query myQuery = mDatabaseRef.orderByChild("medicine");
myQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
medicinesArray = new ArrayList<>();
for (DataSnapshot medicineSnapshot : dataSnapshot.getChildren()) {
String medicineName = medicineSnapshot.child("medicine").getValue(String.class);
medicinesArray.add(medicineName);
}
autoCompletionAdapter = new ArrayAdapter<>
(Prescription.this, android.R.layout.select_dialog_item, medicinesArray);
autoCompletionAdapter.notifyDataSetChanged(); // refresh adapter --- tried this
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
. . .
AutoCompleteTextView testview = (AutoCompleteTextView) findViewById(R.id.test_autocomplete);
testview.setThreshold(2);
testview.setAdapter(autoCompletionAdapter);