如何在 Android 的 edittext 中添加搜索功能

how to add search functionality in edittext in Android

我想在 android 中添加搜索功能。每当我输入任何字母时,所有以该字母开头的联系人都会显示在列表视图中,如下所示:

如何在联系人列表中搜索这个输入的字母?如何在列表视图中显示搜索结果,如图所示?

我知道这可以通过 AutoCompleteTextView 完成,但我无法搜索联系人。我该怎么做?

您可以使用 AutoCompleteTextView 有帮助 link : http://www.javatpoint.com/android-autocompletetextview-example

加载数组列表或字符串数​​组中的所有联系人,然后使用AutoCompleteTextView添加搜索功能

像这样获取所有联系人here

public ArrayList<PhoneContactInfo> getAllPhoneContacts() {

    Log.d("START","Getting all Contacts");
    ArrayList<PhoneContactInfo> arrContacts = new ArrayList<PhoneContactInfo>();
    PhoneContactInfo phoneContactInfo=null;     
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    Cursor cursor = context.getContentResolver().query(uri, new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone._ID}, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
    cursor.moveToFirst();
    while (cursor.isAfterLast() == false)
    {
        String contactNumber= cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  
        String contactName =  cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        int phoneContactID = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));


        phoneContactInfo = new PhoneContactInfo();
        phoneContactInfo.setPhoneContactID(phoneContactID);             
        phoneContactInfo.setContactName(contactName);                   
        phoneContactInfo.setContactNumber(contactNumber); 
        if (phoneContactInfo != null)
        {
            arrContacts.add(phoneContactInfo);
        }
        phoneContactInfo = null; 
        cursor.moveToNext();
    }       
    cursor.close();
    cursor = null;
    Log.d("END","Got all Contacts");
    return arrContacts;
}