Android - AutoCompleteTextView 通配符建议 [英] Android - AutoCompleteTextView wildcard suggestion

查看:32
本文介绍了Android - AutoCompleteTextView 通配符建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天.我的 Android 应用程序中有一个 AutoCompleteTextView,它运行良好.但是,我注意到这些建议基于提供给 AutoCompleteTextView 的列表子字符串的第一个字符.这本身很好,但是,我想要的是它也显示包含用户输入的项目.

Good day. I have an AutoCompleteTextView in my Android Application, and it is working fine. However, I noticed that the suggestions are based on the first character(s) of the substrings of the list supplied to the AutoCompleteTextView. That is fine on its own, however, what I want is for it to also show the items which contains the user input.

例如,让我们使用这个列表:

For example, let's use this list:

  • 脂肪
  • 坏狼
  • 网络人
  • Daleks

输入 ad 会提示 Adipose,但是,我也希望提示 Bad Wolf,因为它包含 adBad 中的代码>.这不会发生,因为 AutoCompleteTextView 只查看列表项中子字符串的开头(子字符串由空格分隔),而不是在这些子字符串中.

Typing in ad will suggest Adipose, however, I also want Bad Wolf to be suggested since it contains ad in Bad. This won't happen because the AutoCompleteTextView only looks at the beginning of the substrings (substring are separated by a whitespace) in the list items and not within those substrings.

有没有办法让 AutoCompleteTextViews 建议包含输入文本的项目,而不管该文本位于列表项内的哪个位置?

Is there any way to make AutoCompleteTextViews suggest items that contains the input text regardless of where that text falls inside the list item?

感谢您的帮助.

编辑/更新

请在下面查看 pskink 的评论.我尝试实现如下相同的解决方案.

Kindly see pskink's comment below. I tried to implement the same solution as follows.

我推断的逻辑是使用SimpleCursorAdapter,而不是常见的ArrayAdater.然后我为 SimpleCursorAdapter 创建了一个 FilterQueryProvider.使用 FilterQueryProviderrunQuery 方法,我现在可以通过搜索用户的约束输入列表来运行过滤算法.代码如下:

The logic from what I inferred is that a SimpleCursorAdapter is to be used, and not the common ArrayAdater. I then created a FilterQueryProvider to the SimpleCursorAdapter. Using the runQuery method of the FilterQueryProvider, I can now run a filter algorithm by searching my list of the constraint input of the user. Here is the code:

//initialize the ACTV
AutoCompleteTextView search = (AutoCompleteTextView) findViewById(R.id.actvCatalogueSearch);
search.setThreshold(1); //set threshold

//experiment time!!

//I honestly don't know what this is for
int[] to = { android.R.id.text1 };

//initializing the cursorAdapter. 
//please note that pdflist is the array I used for the ACTV value
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, 
        android.R.layout.simple_dropdown_item_1line, null, pdflist, to, 0);

cursorAdapter.setStringConversionColumn(1);

//FilterQueryProvider here
FilterQueryProvider provider = new FilterQueryProvider(){
    @Override
    public Cursor runQuery(CharSequence constraint) {
        // TODO Auto-generated method stub
        Log.d("hi", "runQuery constraint: " + constraint);
        if (constraint == null) {
            return null;
        }
        String[] columnNames = { Columns._ID, "name" };
        MatrixCursor c = new MatrixCursor(columnNames);

        try {
            //loop through the array, then when an array element contains the constraint, add.
            for (int i = 0; i < pdflist.length; i++) {
                if(pdflist[i].contains(constraint)){
                    c.newRow().add(i).add(pdflist[i]);  
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c;
    }
};

cursorAdapter.setFilterQueryProvider(provider);
search.setAdapter(cursorAdapter);

显示了 runQuery 约束 的 Log 语句,但是,在那之后,应用程序崩溃并且我在我的 logcat 中收到此错误:

The Log statement for the runQuery constraint is shown, however, after that, the app crashes and I get this error in my logcat:

requesting column name with table name -- <first element of array here> 
.....
java.lang.IllegalArugmentException: column <first element of array here> does not exist

单击 logCat 错误行会打开 jar 文件,并且它们中的任何一个都没有指向代码中的一行.但是,从一些错误行来看,我认为我使用 String[] columnNamesMatrixCursor 变量的方式有问题.

Clicking on the logCat error lines opens up the jar file and not any of them point to a line in the code. However, judging from some of the error lines, I think that there's something wrong with how I used the String[] columnNames and the MatrixCursor variables.

有人可以帮忙吗?我之前没有使用过带游标适配器的过滤器查询提供程序,所以我对如何继续使用这个程序一无所知.

Can anyone help? I haven't used Filter Query Providers with Cursor Adapters before so I'm very clueless on how to proceed with this one.

非常感谢任何帮助.谢谢.

Any help is very much appreciated. Thank you.

推荐答案

好的,这就是我让它工作的方式.主要道具以 pskink 为主角.它与我上面的代码非常相似,做了一些更改以使 runQuery 方法工作.

Okay here's how I made it work. Major props to pskink for the lead. It's very similar to the code I have above, with some changes to make the runQuery method work.

使用相同的逻辑/思维模式,只是我更改了 runQuery 方法.阅读评论以进行演练.

The same logic/thought pattern is used, only that I changed the runQuery method. Read the comments for a walkthrough.

//create ACTV Here
AutoCompleteTextView search = (AutoCompleteTextView) findViewById(R.id.actvCatalogueSearch);
search.setThreshold(1);

//I don't know what these are for, honestly.
String[] from = { "name" };
int[] to = { android.R.id.text1 };

//create a simple cursorAdapter
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, 
        android.R.layout.simple_dropdown_item_1line, null, from, to, 0);

//again, I don't know what this is for
cursorAdapter.setStringConversionColumn(1);

//create the filter query provider
FilterQueryProvider provider = new FilterQueryProvider(){
    @Override
    public Cursor runQuery(CharSequence constraint) {
        // TODO Auto-generated method stub
        //I need to do this because my list items are in all caps
        String constrain = (String) constraint;
        constrain = constrain.toUpperCase(); 

        if (constraint == null) {
            return null;
        }

        //I'll be honest again, no clue what these lines do. 
        String[] columnNames = { Columns._ID, "name" };
        MatrixCursor c = new MatrixCursor(columnNames);

        try {
            //here's what I do, I go though my Array (pdflist)
            //when a list item contains the user input, I add that to the Matrix Cursor
            //this matrix cursor will be returned and the contents will be displayed 
            for (int i = 0; i < pdflist.length; i++) {
                if(pdflist[i].contains(constrain)){
                    c.newRow().add(i).add(pdflist[i]);  
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c;
    }
};

//use the filter query provider on the cursor adapter
cursorAdapter.setFilterQueryProvider(provider);

//finally, use the adapter on your ACTV
search.setAdapter(cursorAdapter);

这是一项工作,但它完成了工作.老实说,我有点惊讶没有直接/直观"的方式来做到这一点.只需在 AutoCompleteTextView 中启用/禁用某些内容即可完成.

It's a bit of a work but it gets the job done. Honestly, I'm a bit surprised that there's no "straightforward/intuitive" way of doing this. Something to the tune of just enabling/disabling something in your AutoCompleteTextView then it's done.

我想我们必须坚持使用这个解决方案,直到另行通知.

I guess we'll have to stick with this solution until further notice.

这篇关于Android - AutoCompleteTextView 通配符建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆