使用BaseAdapter android填充AutoCompleteTextView

Ris*_*shi 4 android

有人可以给我一个链接,使用Android中的BaseAdapter为手机联系人填写AutoCompleteTextView.

谢谢

Mar*_*ark 11

user750716错了.您可以使用BaseAdapter填充AutoCompleteTextView.您只需记住BaseAdapter必须实现Filterable.

在适配器中创建对象的ArrayList.使用您想要的任何视图实现getView并使用objects.get(position)info填充它.实现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();
    }

}
Run Code Online (Sandbox Code Playgroud)