[英]how to add search functionality in edittext in Android
I want to add search functionality in android .我想在 android 中添加搜索功能。 Whenever I enter the any letter all contacts started with this letter will be showed in an listview which is as follows :每当我输入任何字母时,所有以此字母开头的联系人都将显示在列表视图中,如下所示:
How can I search this entered letter in contact list ?如何在联系人列表中搜索此输入的字母? How can I show search result in an listview as in picture ?如何在列表视图中显示搜索结果,如图所示?
I know this can be done AutoCompleteTextView But I can not search over contacts .我知道这可以做到 AutoCompleteTextView 但我无法搜索联系人。 How can I do this ?我怎样才能做到这一点 ?
Load all the contacts in array list or string array and then use AutoCompleteTextView for adding search functionallity加载数组列表或字符串数组中的所有联系人,然后使用AutoCompleteTextView添加搜索功能
Gett all contacts like this as expalined here获取所有这样的联系人,如解释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;
}
您可以使用 AutoCompleteTextView 获取有用的链接: http : //www.javatpoint.com/android-autocompletetextview-example
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.