【问题标题】:Android - AutoCompleteTextView wildcard suggestionAndroid - AutoCompleteTextView 通配符建议
【发布时间】:2014-11-26 13:55:49
【问题描述】:

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

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

  • 脂肪
  • 坏狼
  • 网络人
  • 戴尔克斯

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

有没有办法让 AutoCompleteTextViews 建议包含输入文本的项目,而不管该文本在列表项中的什么位置?

感谢您的帮助。

编辑/更新

请在下面查看 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 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

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

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

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

【问题讨论】:

  • @pskink,感谢您的链接。我尝试在链接中实现您的答案,但是遇到了问题。现在编辑我的问题。
  • 那么你的问题到底是什么?
  • 我似乎无法使runQuery 正常工作。请查看编辑。
  • "from" 参数是列的列表,而不是行

标签: android autocompletetextview


【解决方案1】:

好的,这就是我的工作方式。 pskink 的主要道具。 它与我上面的代码非常相似,只是进行了一些更改以使 runQuery 方法起作用。

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

//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 中启用/禁用某些东西就可以了。

我想我们将不得不坚持这个解决方案,直到另行通知。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    相关资源
    最近更新 更多