Android - AutoCompleteTextView通配符建议

Raz*_*riz 5 android autocompletetextview

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

例如,让我们使用此列表:

  • 脂肪
  • 坏狼
  • Cyber​​men
  • 的Daleks

然而,打字ad会建议Adipose我也想Bad Wolf提出建议,因为它包含adBad.这不会发生,因为AutoCompleteTextView只查看列表项中的子字符串的开头(子字符串由空格分隔),而不是在这些子字符串中.

是否有任何方法可以使AutoCompleteTextViews建议包含输入文本的项目,无论该文本在列表项目中的位置如何?

谢谢你的帮助.

编辑/ UPDATE

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

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

//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);
Run Code Online (Sandbox Code Playgroud)

显示了该语句的Log语句runQuery constraint,然后,应用程序崩溃,我在logcat中收到此错误:

requesting column name with table name -- <first element of array here> 
.....
java.lang.IllegalArugmentException: column <first element of array here> does not exist
Run Code Online (Sandbox Code Playgroud)

单击logCat错误行将打开jar文件,而不是任何指向代码中的一行.但是,根据一些错误行判断,我认为我使用String[] columnNamesMatrixCursor变量的方式有问题.

有人可以帮忙吗?我之前没有使用过滤查询提供程序和游标适配器,所以我对如何继续这个过程非常无能为力.

很感谢任何形式的帮助.谢谢.

Raz*_*riz 3

好吧,这就是我如何让它发挥作用的。pskink 取得领先的主要道具。它与我上面的代码非常相似,只是进行了一些更改以使该runQuery方法起作用。

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

//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);
Run Code Online (Sandbox Code Playgroud)

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

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