在Android中扩展过滤器时如何使用publishResults()方法?
我正在开发一个自动完成文本视图,它将在键值系统上工作,并试图找出我需要做什么才能使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();
}
}
}
}
I'm working on an autocompletetextview that will work off of a key value system, and am trying to find out what I need to do to make publishResults work, as the results param being passed to publishResults here is correct in the debugger, however I have no idea what it should correspond to or how to cause it to display the results, can anyone help? the creation of this object is in another file, and looks like this:
autoCompleteBox.setAdapter(new AutoCmpAdapter(this, android.R.layout.simple_dropdown_item_1line));
and the rest of the code is as follows:
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();
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先不要使用字符串数组。
要处理键值对,您可以调整 If 语句。
中尝试这个
在你的 onCreate和适配器类
First of all don't use String array.
to work for key value pair you can adjust your If statement..
try this in your onCreate
and adapter class like