在android自动完成文本视图中覆盖过滤器结果?

2024-02-01

这几天我一直在纠结这个问题,我试图在android中设置一个自动完成文本视图,其中用户输入一个键,自动完成建议是值,但是我现在已经尝试了大约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;
    }
}

int 构造函数, 公共NewArrayAdapter(上下文上下文,int资源,int textViewResourceId,列表对象) 第二个参数是 autocompletetextview,第三个参数是嵌套的 TextView,第四个参数是对列表的引用,我只能假设最终应该是结果集,但显然不是......这让我发疯,有没有人有什么建议?我的主要问题是结果必须基于键,而不是值,例如输入“a”可能意味着我在这里尝试做的事情的“tiddlywinks”的结果 任何信息都会很棒,非常感谢


首先为 edittext 设置 textwatcher。喜欢 EditText.addTextChangedListener(searchInput TextWatcher);

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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在android自动完成文本视图中覆盖过滤器结果? 的相关文章

随机推荐