使用BaseAdapter在Android中填充AutoCompleteTextView

4

请问有人可以给我一个链接,在Android中使用BaseAdapter填充AutoCompleteTextView以显示手机联系人吗?

谢谢!


你能帮我解决这个问题吗?https://dev59.com/HIjca4cB1Zd3GeqP07xf - user4050065
2个回答

11

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

在适配器中创建对象的ArrayList。 使用任何你想要的View实现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();
    }

}

感谢您在漫长的等待后给出了答案。 - Rishi
你能帮我解决这个问题吗?https://dev59.com/HIjca4cB1Zd3GeqP07xf - user4050065

1

使用BaseAdapter无法实现这一点,但您可以使用没有SQLite数据库的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" });

我已经在我的形态分析器中完成了这个。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接