Android-AutoCompleteTextView通配符建议
你好。我的Android应用程序中有一个AutoCompleteTextView,它运行良好。但是,我注意到这些建议是基于提供给AutoCompleteTextView的列表子字符串的第一个字符。这本身很好,但是,我希望它也显示包含用户输入的项目 例如,让我们使用以下列表:Android-AutoCompleteTextView通配符建议,android,autocompletetextview,Android,Autocompletetextview,你好。我的Android应用程序中有一个AutoCompleteTextView,它运行良好。但是,我注意到这些建议是基于提供给AutoCompleteTextView的列表子字符串的第一个字符。这本身很好,但是,我希望它也显示包含用户输入的项目 例如,让我们使用以下列表: 脂肪 恶狼 赛博人 戴尔克斯 键入ad将建议Adipose,但是,我也希望Bad Wolf被建议,因为它在Bad中包含ad。这不会发生,因为AutoCompleteTextView只查看列表项中子字符串的开头(子字符串由
- 脂肪
- 恶狼
- 赛博人
- 戴尔克斯
ad
将建议Adipose
,但是,我也希望Bad Wolf
被建议,因为它在Bad
中包含ad
。这不会发生,因为AutoCompleteTextView只查看列表项中子字符串的开头(子字符串由空格分隔),而不查看这些子字符串
有没有办法让AutoCompleteTextView建议包含输入文本的项目,而不管该文本在列表项中的什么位置
谢谢你的帮助
编辑/更新
请参阅下面的pskink评论。我尝试实现如下相同的解决方案
我推断的逻辑是使用SimpleCursorAdapter
,而不是常用的ArrayAdater
。然后,我创建了一个FilterQueryProvider
到SimpleCursorAdapter
。使用FilterQueryProvider
的runQuery
方法,我现在可以通过搜索用户的约束输入列表来运行过滤算法。代码如下:
//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);
单击logCat错误行打开jar文件,其中没有任何一行指向代码中的一行。但是,从一些错误行判断,我认为我使用String[]columnNames
和MatrixCursor
变量的方式有问题
有人能帮忙吗?我以前没有将过滤器查询提供程序与游标适配器一起使用过,因此我对如何继续使用这一提供程序一无所知
非常感谢您的帮助。谢谢。好的,我是这样做的。以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);
//在此处创建ACTV
AutoCompleteTextView搜索=(AutoCompleteTextView)findViewById(R.id.ActvCatalogSearch);
搜索。设置阈值(1);
//老实说,我不知道这些是干什么用的。
字符串[]from={“name”};
int[]to={android.R.id.text1};
//创建一个简单的游标适配器
SimpleCursorAdapter cursorAdapter=新的SimpleCursorAdapter(此,
android.R.layout.simple_下拉列表_项目_1line,null,from,to,0);
//再说一遍,我不知道这是干什么用的
游标适配器。设置字符串转换列(1);
//创建筛选器查询提供程序
FilterQueryProvider=新的FilterQueryProvider(){
@凌驾
公共游标运行查询(CharSequence约束){
//TODO自动生成的方法存根
//我需要这样做,因为我的列表项都是大写的
字符串约束=(字符串)约束;
constraint=constraint.toUpperCase();
if(约束==null){
返回null;
}
//我还是老实说,不知道这些台词是干什么的。
字符串[]columnNames={Columns.\u ID,“name”};
MatrixCursor c=新MatrixCursor(列名称);
试一试{
//下面是我要做的,我遍历我的数组(pdflist)
//当列表项包含用户输入时,我将其添加到矩阵光标中
//将返回该矩阵光标并显示其内容
对于(int i=0;i
这是一项工作,但它完成了任务。老实说,我有点惊讶,没有“直接/直观”的方法来做到这一点。只需在AutoCompleteTextView中启用/禁用某些内容即可
我想我们必须坚持使用此解决方案,直到另行通知。请参阅@pskink,谢谢链接。我尝试在链接中实现您的答案,但是,我遇到了一个问题。现在正在编辑我的问题。那么你到底有什么问题?我似乎无法使
运行查询
正常工作。请参见编辑。“from”参数是列列表,而不是行列表
//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);