【问题标题】:Searchview searches only whole words instead searching in contentSearchview 仅搜索整个单词,而不是在内容中搜索
【发布时间】:2013-05-10 09:14:54
【问题描述】:

我有一个在我的 Arrayadapter 中搜索的搜索字段

   inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            ZT.this.adapter.getFilter().filter(cs);  
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {


        }

        @Override
        public void afterTextChanged(Editable arg0) {

        }
    });

它只搜索整个单词。例如:

Baloon

搜索Bal 返回Baloon,搜索oon 不返回任何内容。在这种情况下,我希望他能返回Baloon

感谢您的帮助。

【问题讨论】:

  • 它是因为它基于起始字符进行搜索。不是任何其他地方。
  • 我怎样才能改变这个,添加一个自定义过滤器?

标签: android arraylist textwatcher android-search


【解决方案1】:

您可以使用 FilterQueryProvider 并将其传递给 adapter.setFilterQueryProvider()。

adapter.setFilterQueryProvider(new FilterQueryProvider() {
    public Cursor runQuery(CharSequence constraint) {
        String s = '%' + constraint.toString() + '%';
        return getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
            new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER},
            Phone.DISPLAY_NAME + ' LIKE ? OR ' + Phone.NUMBER + ' LIKE ?',
            new String[] { s, s },
            null);
    }
});

将此与适配器一起使用。

【讨论】:

    猜你喜欢
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 2022-12-22
    • 1970-01-01
    相关资源
    最近更新 更多