安卓 - AutoCompleteTextView 通配符建议

5

你好。我在我的Android应用程序中使用了AutoCompleteTextView,它运行良好。但是,我注意到建议是基于提供给AutoCompleteTextView的列表子字符串的前几个字符。这本身没问题,但是我希望它也显示包含用户输入的项目。

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

  • Adipose
  • Bad Wolf
  • Cybermen
  • Daleks

输入ad将建议Adipose,但是我还想建议Bad Wolf,因为它在Bad中包含ad。这不会发生,因为AutoCompleteTextView仅查看列表项子字符串(子字符串由空格分隔)的开头,而不是在这些子字符串内部。

有没有办法使AutoCompleteTextView建议包含输入文本的项目,而不管该文本在列表项内的位置如何?

感谢任何帮助。

编辑/更新

请参见pskink的下面的评论。我尝试实现以下相同的解决方案。

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

//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约束的日志语句,然而,在此之后,应用程序崩溃并在我的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 [] columnNames" 和 "MatrixCursor" 变量时存在问题。
有人可以帮忙吗?我以前没有使用过 Filter Query Providers 和 Cursor Adapters,所以我对如何处理这个问题非常无助。
非常感谢任何帮助。谢谢。

请参见以下网址:https://dev59.com/0mIj5IYBdhLWcg3wzIJo?answertab=active#tab-top - pskink
@pskink,感谢提供链接。我尝试了你在链接中的答案,但是遇到了问题。现在正在编辑我的问题。 - Razgriz
那么,你实际上有什么问题? - pskink
我似乎无法使runQuery正常工作。请查看编辑。 - Razgriz
那我该怎么使用呢?因为我只是查看一个字符串数组。 - Razgriz
显示剩余4条评论
1个回答

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);

这有些费力,但它能完成任务。说实话,我有点惊讶没有“直接/直观”的方法来做到这一点。大概需要在AutoCompleteTextView中启用/禁用某些东西,然后就完成了。

我想我们将不得不继续使用这个解决方案,直至另行通知。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接