AutoComplete TextView with Contacts

前端 未结 2 1916
不知归路
不知归路 2020-12-29 16:37

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 textb

2条回答
  •  情话喂你
    2020-12-29 17:21

    Try the following:

    ArrayList emailAddressCollection = new ArrayList();
    
    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 adapter = new ArrayAdapter(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:

    
    

提交回复
热议问题