在android自动完成文本视图中覆盖过滤器结果?
这几天我一直在纠结这个问题,我试图在 android 中设置一个自动完成文本视图,其中用户输入一个键,自动完成建议是值,但是我现在已经尝试了大约 10 种不同的方法,扩展BaseAdapter、SimpleAdapter 和现在的 ArrayAdapter,我通过调试器注意到我的结果集很好,但是我真的不知道我应该在代码的publishResults() 部分做什么。第一个参数是使用以下 XML 的自定义 autocompletetextview 控件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/txtInnerView2" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
并且该类如下所示:
public class NewArrayAdapter<T> extends ArrayAdapter implements Filterable {
ArrayList<String> allWords;
ArrayList<String> resultWords;
String value[] = { "Awesome", "Bear", "Cat", "Dog" };
String key[] = { "A", "B", "C", "D" };
public NewArrayAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
// TODO Auto-generated constructor stub
allWords = new ArrayList<String>();
resultWords = new ArrayList<String>();
}
@Override
public Filter getFilter() {
Filter custom_filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults f = new FilterResults();
if (constraint != null) {
ArrayList<String> lstResults = new ArrayList<String>();
for (int x = 0; x < key.length; x++) {
if (key[x].startsWith(constraint.toString().toUpperCase())) {
lstResults.add(value[x]);
}
}
f.values = lstResults;
f.count = lstResults.size();
}
return f;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
resultWords.clear();
if (results.count > 0) {
resultWords.addAll((Collection<? extends String>) results.values);
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return custom_filter;
}
}
int 构造函数, 公共NewArrayAdapter(上下文上下文,int资源,int textViewResourceId,列表对象) 第二个参数是 autocompletetextview,第三个参数是嵌套的 TextView,第四个参数是对列表的引用,我只能假设最终应该是结果集,但显然不是......这让我发疯,有人有什么建议吗?我的主要问题是结果必须基于键,而不是值,例如,输入“a”可能意味着我在这里尝试执行的操作的“tiddlywinks”结果 任何信息都会很棒,非常感谢
i've been pulling my hair over this a few days, I'm trying to setup an autocompletetextview in android where the user inputs a key and the autocomplete suggestions are the values, however I've tried this about 10 different ways now, extending BaseAdapter, SimpleAdapter and now ArrayAdapter, and I've noticed via the debugger that my resultset is fine, however I really have no idea what i'm supposed to be doing in the publishResults() section of the code. The first argument is custom autocompletetextview control using the following XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/txtInnerView2" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
and the class looks like this:
public class NewArrayAdapter<T> extends ArrayAdapter implements Filterable {
ArrayList<String> allWords;
ArrayList<String> resultWords;
String value[] = { "Awesome", "Bear", "Cat", "Dog" };
String key[] = { "A", "B", "C", "D" };
public NewArrayAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
// TODO Auto-generated constructor stub
allWords = new ArrayList<String>();
resultWords = new ArrayList<String>();
}
@Override
public Filter getFilter() {
Filter custom_filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults f = new FilterResults();
if (constraint != null) {
ArrayList<String> lstResults = new ArrayList<String>();
for (int x = 0; x < key.length; x++) {
if (key[x].startsWith(constraint.toString().toUpperCase())) {
lstResults.add(value[x]);
}
}
f.values = lstResults;
f.count = lstResults.size();
}
return f;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
resultWords.clear();
if (results.count > 0) {
resultWords.addAll((Collection<? extends String>) results.values);
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return custom_filter;
}
}
int the constructor,
public NewArrayAdapter(Context context, int resource, int textViewResourceId, List objects)
the 2nd argument is the autocompletetextview, the 3rd is the nested TextView, and the 4th is a reference to the List that i can only assume is what eventually should be the resultset, but apparently isn't... this is driving me nuts, does anyone have any suggestions? My main issue is that the results have to be based on the key, not the value, e.g. typing "a" could mean a result of "tiddlywinks" for what i'm trying to do here
any info would be great, thanks very much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先设置文本观察器来编辑文本。喜欢
urEditText.addTextChangedListener(searchInputTextWatcher);
其中 someAdapter 是您的适配器,您在其中实现方法 filterList(搜索字段中的单词类型)
First of you set textwatcher for edit text. like
urEditText.addTextChangedListener(searchInputTextWatcher);
where someAdapter is a your adapter where u implement method filterList(word type in search field)