我已经在这几天拉我的头发,我试图在android中设置一个autocompletetextview,其中用户输入一个键和自动完成建议是值,但我已经尝试了10种不同的方式现在,扩展了BaseAdapter,SimpleAdapter和现在的ArrayAdapter,并且我通过调试器注意到我的结果集很好,但是我真的不知道我应该在代码的publishResults()部分中做什么。第一个参数是自定义使用以下XML autocompletetextview控制:覆盖android自动完成文本视图中的filterresults?
<?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,第四个是对List的引用,我只能假定最终应该是结果集,但显然不是......这是驱使我疯了,有没有人有任何建议?我的主要问题是结果必须基于关键而不是价值,例如打字“a”可能意味着我想要在这里做的“tiddlywinks”的结果 任何信息都会很好,非常感谢
我知道这是两岁,但应该在函数调用filterList匹配someAdapter.filterList (序列); – Mike 2015-02-02 20:23:15