列表视图过滤器的Andr​​oid [英] List View Filter Android

查看:240
本文介绍了列表视图过滤器的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了Android的列表视图,我想添加列表上方的编辑文本,当用户输入文本的名单将被过滤根据用户输入

I have created a list view in android and I want to add edit text above the list and when the user enter text the list will be filtered according to user input

谁能告诉我请,如果有一种方法来筛选的android列表适配器?

can anyone tell me please if there is a way to filter the list adapter in android ?

推荐答案

是的。 添加一个EditText在其反洗钱布局文件的列表视图的顶部。 而在活动/片段。

Yes. Add an EditText on top of your listview in its aml layout file. And in your activity/fragment..

lv = (ListView) findViewById(R.id.list_view);
    inputSearch = (EditText) findViewById(R.id.inputSearch);

// Adding items to listview
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name,    products);
lv.setAdapter(adapter);       
inputSearch.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
        // When user changed the Text
        MainActivity.this.adapter.getFilter().filter(cs);
    }

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

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

在这里基本是添加一个 OnTextChangeListener 您编辑文本及其回调方法里面将过滤器应用于您的列表视图的适配器。

The basic here is to add an OnTextChangeListener to your edit text and inside its callback method apply filter to your listview's adapter.

修改

要获得过滤器自定义BaseAdapter你会需要实施可筛选接口。

To get filter to your custom BaseAdapter you"ll need to implement Filterable interface.

class CustomAdapter extends BaseAdapter implements Filterable {

    public View getView(){
    ...
    }
    public Integer getCount()
    {
    ...
    }

    @Override
    public Filter getFilter() {

        Filter filter = new Filter() {

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                arrayListNames = (List<String>) results.values;
                notifyDataSetChanged();
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                FilterResults results = new FilterResults();
                ArrayList<String> FilteredArrayNames = new ArrayList<String>();

                // perform your search here using the searchConstraint String.

                constraint = constraint.toString().toLowerCase();
                for (int i = 0; i < mDatabaseOfNames.size(); i++) {
                    String dataNames = mDatabaseOfNames.get(i);
                    if (dataNames.toLowerCase().startsWith(constraint.toString()))  {
                        FilteredArrayNames.add(dataNames);
                    }
                }

                results.count = FilteredArrayNames.size();
                results.values = FilteredArrayNames;
                Log.e("VALUES", results.values.toString());

                return results;
            }
        };

        return filter;
    }
}

perfromFiltering()您需要做的搜索查询的实际比较在数据库中的值。将其结果传递给 publishResults()方式。

Inside perfromFiltering() you need to do actual comparison of the search query to values in your database. It will pass its result to publishResults() method.

这篇关于列表视图过滤器的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆