Krishna
Krishna

Reputation: 4984

How to load contacts in Autocompleate Textview In background?

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

Upvotes: 0

Views: 232

Answers (1)

s.d
s.d

Reputation: 29436

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();  

Upvotes: 1

Related Questions