0

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);
KENdi
  • 7,576
  • 2
  • 16
  • 31
Rajesh
  • 77
  • 5
  • Please make sure your question includes the [minimal code that is needed to reproduce the problem](http://stackoverflow.com/help/mcve) (read the link, it is really quite useful). Posting such an MCVE is by far the most efficient way to get help on Stack Overflow. – Frank van Puffelen Jul 31 '17 at 17:55
  • Set the adapter in onDataserchanged – Saurabh Padwekar Jul 31 '17 at 18:10
  • It works fine if I set the adapter inside onDataChange. But I am adding AutoCompleteTextView's dynamically on click of a button, so can't set the adapter inside onDataChange. – Rajesh Jul 31 '17 at 18:14

0 Answers0