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

查看:49
本文介绍了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 WolfBad中包含ad.这不会发生,因为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 constraint的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天全站免登陆