如何在ListView正确地过滤文字? [英] How to filter text in a ListView properly?

查看:165
本文介绍了如何在ListView正确地过滤文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的EditText框筛选我的ListView,但是目前,它不工作。

I've tried to filter my ListView from my EditText box, but currently, it's not working.

我有得到正确的数据一个ListView,他们将被添加到我自己的ListView使用一个ArrayAdapter类和我自己的ArrayList类。当我例如,键入表我希望它从我的ListView加载的标题排序和比它应该告诉我,留与匹配表项。

I have a ListView that get data properly and they are added to my own ListView using a ArrayAdapter class and my own ArrayList class. When I for example, typing in "table" I want it to sort from the loaded titles in my ListView and than it should show me the items that are left and matching table.

我目前的code:

private ArrayList<Order> orders; /* ArrayList class with my own Order class to 
define title, info etc. */
private OrderAdapter adapter; //Own class that extends ArrayAdapter

orders = new ArrayList<Order>();
adapter = new OrderAdapter(this, R.layout.listrow, orders); /* listrow defining a
single item in my ListView */
setListAdapter(adapter); //Set our adapter to a ListView

search.addTextChangedListener(filterTextWatcher); //search is my EditText

private TextWatcher filterTextWatcher = new TextWatcher() {

    public void afterTextChanged(Editable s) {

    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        adapter.getFilter().filter(s); //Filter from my adapter
        adapter.notifyDataSetChanged(); //Update my view
    }

};

好了,我怎么能做到这一点?当我输入一些值插入的EditText框一切消失,即使存在匹配

Okay, so how can I achieve this? When I entering some value into the EditText box everything disappear, even if there is a match.

在此先感谢,并告诉我,如果你需要更多的code片段或者,如果这个问题我不清楚!

Thanks in advance and tell me if you need more code snippets or if this question i unclear!

推荐答案

内置的由ArrayAdapter实现过滤器通过调用toString()方法中包含的对象转换为字符串。正是这种字符串将被用来执行匹配。如果你只是想在您的订单对象匹配一个字符串字段,可以覆盖toString()方法的订单类返回现场。如果要执行更灵活的匹配,如匹配多个字段,请查看这篇文章展示了如何创建自定义过滤器:

The built-in filter implemented by ArrayAdapter converts the contained object to string by calling toString() method. It is this string that will be used to perform the matching. If you are only trying to match one string field in your Order object, you can override the toString() method for your Order class to return that field. If you want to perform more flexible matching, such as matching multiple fields, check out this post which shows how to create a custom filter:

<一个href="http://stackoverflow.com/questions/2718202/custom-filtering-in-android-using-arrayadapter">http://stackoverflow.com/questions/2718202/custom-filtering-in-android-using-arrayadapter

这篇关于如何在ListView正确地过滤文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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