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

时间:2015-11-26 11:55:24

标签: android android-listview

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

enter image description here

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

我知道这可以做到AutoCompleteTextView但我无法搜索联系人。我怎样才能做到这一点 ?

2 个答案:

答案 0 :(得分:1)

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

将所有此类联系人称为expalined 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;
}

答案 1 :(得分:0)

您可以使用AutoCompleteTextView 有用的链接:http://www.javatpoint.com/android-autocompletetextview-example