Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoComplete TextView with Contacts

i need to display an auto complete text box which will basically load the contacts e-mail ids. I have tried it using a custom adapter but nothing gets populated in the textbox. No suggestions at all. Any solutions will be very useful.

like image 676
SKen Avatar asked Jan 16 '23 20:01

SKen


1 Answers

Try the following:

ArrayList<String> emailAddressCollection = new ArrayList<String>();

ContentResolver cr = getContentResolver();

Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null, null, null);

while (emailCur.moveToNext())
{
    String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            emailAddressCollection.add(email);
}
emailCur.close();

String[] emailAddresses = new String[emailAddressCollection.size()];
emailAddressCollection.toArray(emailAddresses);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
             android.R.layout.simple_dropdown_item_1line, emailAddresses);
AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.YOUR_TEXT_VIEW);
     textView.setAdapter(adapter);
 }

Note: Don't forget to add the READ_CONTACTS permission to your Manifest.xml:

<uses-permission android:name="android.permission.READ_CONTACTS" />
like image 128
Korhan Ozturk Avatar answered Jan 23 '23 10:01

Korhan Ozturk