列表视图过滤器 Android [英] List View Filter Android

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

问题描述

我在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 ?

推荐答案

在列表视图的 .xml 布局文件中添加一个 EditText.在您的活动/片段中..

Add an EditText on top of your listview in its .xml 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,您需要实现 Filterable 接口.

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;
    }
}

performFiltering() 中,您需要将搜索查询与数据库中的值进行实际比较.它将其结果传递给 publishResults() 方法.

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

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

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