【问题标题】:how to add search functionality in edittext in Android如何在 Android 的 edittext 中添加搜索功能
【发布时间】:2015-11-26 11:55:23
【问题描述】:

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

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

我知道这可以做到 AutoCompleteTextView 但我无法搜索联系人。我该怎么做?

【问题讨论】:

  • 你问的是AutoCompleteTextView
  • 视情况而定.... SearchView 可用于操作栏/工具栏。至于简单的 EditText,您可以使用 @1615903 答案、Frank 的 AutoCompleteTextView,或者如果查询需要来自服务器,您可以使用 addTextChangedListener 来搜索 API。
  • 使用SimpleCursorAdapter,使用ContentResolver#queryContactsContract.Contacts.CONTENT_FILTER_URI 获取您的Cursor
  • 我希望这不是您朋友的真实电话号码列表...

标签: android android-listview


【解决方案1】:

在数组列表或字符串数​​组中加载所有联系人,然后使用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;
}

【讨论】:

  • 如何加载数组列表中的所有联系人?
【解决方案2】:

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

【讨论】:

猜你喜欢
  • 2015-09-12
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-06
  • 2019-01-11
  • 1970-01-01
相关资源
最近更新 更多