我正在研究一个基于键值系统的自动完成文本视图,并试图找出如何使publishResults起作用,因为在调试器中传递给publishResults的结果参数是正确的,但我不知道它应该对应什么或如何显示结果,请问有谁能帮忙吗?创建此对象在另一个文件中进行,看起来像这样:
autoCompleteBox.setAdapter(new AutoCmpAdapter(this, android.R.layout.simple_dropdown_item_1line));
而其余的代码如下所示:
public class AutoCmpAdapter extends ArrayAdapter<String> implements Filterable {
protected Filter filter;
protected ArrayList<String> items;
protected ArrayList<String> res;
String lWds[] = { "HOMER", "TOM" };
String sWds[] = { "SIMPSON", "JONES" };
public AutoCmpAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
filter = new PhysFilter();
res = new ArrayList<String>();
}
public Filter getFilter() {
return filter;
}
private class PhysFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults f = new FilterResults();
res.clear();
if (constraint != null) {
ArrayList<String> res = new ArrayList<String>();
for (int x = 0; x < sWds.length; x++) {
if (sWds[x].toUpperCase().startsWith(constraint.toString().toUpperCase())) {
res.add(lWds[x]);
}
}
f.values = res.toArray();
f.count = res.size();
}
return f;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.count > 0) {
Log.println(Log.INFO, "Results", "FOUND");
notifyDataSetChanged();
} else {
Log.println(Log.INFO, "Results", "-");
notifyDataSetInvalidated();
}
}
}
}
constraint
为空时,您可能希望返回所有值,而不是没有值 - 因为通常没有限制意味着“没有搜索”,而不是“搜索无内容”。这也将简化您的publishResults()代码。例如,您可以将performFiltering()更改为:``if (constraint == null || constraint.length() == 0) { results.count = listData.size(); results.values = listData;} else {/执行搜索/} - Mtl Dev