在Android AutoCompleteTextView中覆盖过滤结果?

5
我已经为此苦恼了几天,我正在尝试在Android中设置AutocompleteTextView,其中用户输入关键字,自动完成建议是值。然而,我现在已经尝试了10种不同的方法,扩展BaseAdapter、SimpleAdapter和ArrayAdapter,通过调试器发现我的结果集很好,但是我真的不知道我在代码的publishResults()部分应该做什么。第一个参数是使用以下XML自定义AutocompleteTextView控件:
    <?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;
    }
}

在构造函数中, public NewArrayAdapter(Context context, int resource, int textViewResourceId, List objects) 第二个参数是自动完成文本视图,第三个参数是嵌套的TextView,第四个参数是对List的引用。我只能猜测它最终应该是结果集,但显然不是......这让我抓狂,有人有什么建议吗?我的主要问题是结果必须基于键而不是值,例如在这里尝试的内容中,键入"a"可能意味着结果是"tiddlywinks"。任何信息都很好,非常感谢。
1个回答

2

首先,您需要为编辑文本设置TextWatcher,例如:urEditText.addTextChangedListener(searchInputTextWatcher);

private TextWatcher searchInputTextWatcher = new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence sequence, int start, int before,
            int count) {
        //Log.i("View adapter    count",String.valueOf(locationViewAdapter.getCount()));
        //if (locationViewAdapter.getCount()>1)
            someAdapter.filterList(sequence);
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
    }

    @Override
    public void afterTextChanged(Editable arg0) {
    }
};

其中someAdapter是您的适配器,在其中实现方法filterList(输入搜索字段中的单词类型)

 public void filterArrayList(CharSequence sequence) {
    if (TextUtils.isEmpty(sequence)) {
        someArrayList = (ArrayList<type>) cloneCategoryArrayList
                .clone();

    } else {
        someArrayList = (ArrayList<type>) cloneCategoryArrayList
                .clone();
        if (!TextUtils.isEmpty(sequence)) {

            List<type> tempCategoryArrayList = new ArrayList<type>(
                    someArrayList);
            for (type obj : tempCategoryArrayList) {
                String typeName = obj.name;

                if (!typename.toLowerCase().startsWith(
                        sequence.toString().toLowerCase(), 0))
                    somearrayList.remove(type);
            }
        }
    }
    notifyDataSetChanged();
}

我知道这已经是两年前的事了,但是函数应该被称为filterList以匹配someAdapter.filterList(sequence)吗? - Mike

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