使用BaseAdapter android填充AutoCompleteTextView

时间:2011-04-13 12:45:20

标签: android

有人可以给我一个链接,在Android中使用BaseAdapter填充AutoCompleteTextView,用于手机联系。

由于

2 个答案:

答案 0 :(得分: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();
    }

}

答案 1 :(得分:1)

使用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" });

我在我的形态分析仪中完成了这项工作。