从编辑文本中过滤列表视图

Filter list view from edit text(从编辑文本中过滤列表视图)
本文介绍了从编辑文本中过滤列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个作为搜索栏的编辑文本和一个过滤我输入的文本的列表视图,但不幸的是,它不会过滤列表视图.我使用了带有对象 Friend 的自定义数组适配器.朋友对象有名字、地址和电话号码,但我只想过滤它的名字.在我的活动中...

searchBarTextView.addTextChangedListener(new TextWatcher() {@覆盖public void onTextChanged(CharSequence s, int start, int before, int count) {friendListAdapter.getFilter().filter(s);}}

在适配器中...

<前>@覆盖公共过滤器getFilter(){Log.d(TAG, "开始 getFilter");if(newFilter == null) {新过滤器 = 新过滤器() {@覆盖protected void publishResults(CharSequence 约束,FilterResults 结果) {//TODO 自动生成的方法存根Log.d(TAG, "发布结果");notifyDataSetChanged();}

<代码> @Override受保护的FilterResults performFiltering(CharSequence约束){Log.d(TAG, "执行过滤");约束 = 约束.toString().toLowerCase();Log.d(TAG, "约束:"+约束);列出<ChatObject>filtersFriendList = new LinkedList();for(int i=0; i

有人可以帮我如何正确显示过滤后的数组适配器吗?我认为 notifyDataSetChanged 没有被调用.谢谢.

解决方案

我的问题解决了,发现我要重写 getCount()getItem().

I have an edit text as a search bar and a list view that filters the text that I typed but unfortunately, it doesn't filter the list view. I have used an customize array adapter with object Friend. Friend object has name, address and phone number but I only want to filter its name. In my activity...

searchBarTextView.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    friendListAdapter.getFilter().filter(s);
}}

While in adapter...

    @Override
    public Filter getFilter() {
        Log.d(TAG, "begin getFilter");
        if(newFilter == null) {
            newFilter = new Filter() {
                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    // TODO Auto-generated method stub
                    Log.d(TAG, "publishResults");
                    notifyDataSetChanged();
                }

@Override protected FilterResults performFiltering(CharSequence constraint) { Log.d(TAG, "performFiltering"); constraint = constraint.toString().toLowerCase(); Log.d(TAG, "constraint : "+constraint); List<ChatObject> filteredFriendList = new LinkedList<ChatObject>(); for(int i=0; i<friendList.size(); i++) { Friend newFriend = friendList.get(i); Log.d(TAG, "displayName : "+newFriend.getDisplayName().toLowerCase()); if(newFriend.getDisplayName().toLowerCase().contains(constraint)) { Log.d(TAG, "equals : "+newFriend.getDisplayName()); filteredFriendList.add(newFriend); } } FilterResults newFilterResults = new FilterResults(); newFilterResults.count = filteredFriendList.size(); newFilterResults.values = filteredFriendList; return newFilterResults; } }; } Log.d(TAG, "end getFilter"); return newFilter; }

Could someone please help me how to correctly show the filtered array adapter? I think the notifyDataSetChanged is not invoked. Thanks.

解决方案

My problem is solved, found out that I have to override getCount() and getItem().

这篇关于从编辑文本中过滤列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Prevent enter key on EditText but still show the text as multi-line(防止在 EditText 上输入键,但仍将文本显示为多行)
Android keyboard next button issue on EditText(EditText上的Android键盘下一个按钮问题)
When the soft keyboard appears, it makes my EditText field lose focus(当软键盘出现时,它使我的 EditText 字段失去焦点)
android how to make text in an edittext exactly fixed lines(android如何在edittext中制作文本完全固定的行)
How to use regular expression in Android(如何在 Android 中使用正则表达式)
In android how to show asterisk (*) in place of dots in EditText having inputtype as textPassword?(在android中如何显示星号(*)代替EditText中的点,输入类型为textPassword?)