搜索在AutoCompleteTextView中显示重复

时间:2017-08-21 19:42:47

标签: java android autocomplete

我在使用Android的AutoCompleteTextView时遇到问题。

AutoComplete正在向我提供重复数据,因为它正在搜索名称和电子邮件。我希望它只显示一个值,可以通过搜索联系人的姓名或搜索电子邮件。

基本上,我有一个存储HashMap的列表,其中包含每个联系人的姓名和电子邮件:

List<Map<String, String>> contact_list = new ArrayList<>();

要填写Hashmap,我使用Cursor来帮助我从数据库中获取信息:

Cursor cursor = database.rawQuery(ContactTable.SELECT_ALL, null);
    if(cursor.getCount() > 0){
        while(cursor.moveToNext()){
            Map<String, String> contact_info = new HashMap<>();
            contact_info.put("name",     cursor.getString(ContactTable.COLUMN_CONTACT_NAME_IDX));
            contact_info.put("email", cursor.getString(ContactTable.COLUMN_CONTACT_EMAIL_IDX));
            contact_list.add(contact_info);
        }
    }

在我将HashMap的所有实例添加到我的列表后,我将此列表添加到SimpleAdapter并在AutoCompleteTextView中设置此适配器:

    auto_adapter = new SimpleAdapter(
            mContext,
            contact_list,
            android.R.layout.simple_list_item_2,
            new String[]{"name", "email"},
            new int[]{android.R.id.text1, android.R.id.text2});
    auto_complete.setAdapter(auto_adapter);

阈值的值为“1”,并在布局文件中声明:

<AutoCompleteTextView
    android:id="@+id/dialog_participant_name_auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    android:inputType="textCapWords"
    android:hint="@string/dialog_participant_auto_name"
    android:textColorHint="@color/hint_text_color"
    android:completionThreshold="1"
    android:visibility="gone"/>

它的工作方式几乎完美,但它会使查询的值翻倍。例如,如果我保存名为“Fulano”的联系人并发送电子邮件“fulano@gmail.com”并搜索“F”,则AutoCompleteTextView会返回两个项目:

autoComplete problem

0 个答案:

没有答案