问题描述
|
有人可以给我一个链接,该链接使用android中的BaseAdapter填充AutoCompleteTextView进行电话联系。
谢谢
解决方法
user750716错误。
您可以使用BaseAdapter填充AutoCompleteTextView。您只需要记住BaseAdapter必须实现Filterable。
在适配器中,创建对象的ArrayList。
用所需的任何视图实现getView并用objects.get(position)信息填充它。
实现getItem(int position)返回一个字符串(单击对象的名称)
在同一适配器中添加用于过滤的内容:
public Filter getFilter() {
return new MyFilter();
}
private class MyFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence filterString) {
// this will be done in different thread
// so you could even download this data from internet
FilterResults results = new FilterResults();
ArrayList<myObject> allMatching = new ArrayList<myObject>()
// find all matching objects here and add
// them to allMatching,use filterString.
results.values = allMatching;
results.count = allMatching.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
objects.clear();
ArrayList<myObject> allMatching = (ArrayList<myObject>) results.values;
if (allMatching != null && !allMatching.isEmpty()) {
objects = allMatching;
}
notifyDataSetChanged();
}
}
,对于BaseAdapter,这是不可能的,但是您可以在没有SQLite DB的情况下使用CursorAdapter。在函数runQueryOnBackgroundThread中,您可以创建一个MatrixCursor。
像这样:
String[] tableCols = new String[] { \"_id\",\"keyword\" };
MatrixCursor cursor = new MatrixCursor(tableCols);
cursor.addRow(new Object[] { 1,\"went\" });
cursor.addRow(new Object[] { 2,\"gone\" });
我已经在形态分析仪中完成了此操作。