0

I have developed the android application in that I am using Autocompleate textview to load contacts. It is working fine but the activity is getting slow to open because loading all contacts in onCreate method. I want to know how to load the contacts to the Autocmpleate textview in background

Krishna
  • 4,892
  • 18
  • 63
  • 98

1 Answers1

1

Use an Asynctask to initialize the auto complete adapter in background:

    final AutoCompleteTextView vTextView = findViewById(R.id.auto_text);

    new AsyncTask<Void, Void, List<String>>() {
        @Override
        protected List<String> doInBackground(Void... pVoids) {
            List<String> contacts = new ArrayList<String>();

            //--read contacts---

            return contacts;
        }

        @Override
        protected void onPostExecute(List<String> result) {
            ArrayAdapter<String> vAdapter = new ArrayAdapter<String>(getApplicationContext(), 
                    android.R.layout.simple_dropdown_item_1line);
            vAdapter.addAll(result);
            vTextView.setAdapter(vAdapter);
        }
    }.execute();  
S.D.
  • 29,290
  • 3
  • 79
  • 130