从编辑文本中过滤列表视图
我有一个编辑文本作为搜索栏和一个列表视图,用于过滤我键入的文本,但不幸的是,它不会过滤列表视图。我使用了带有对象 Friend 的自定义数组适配器。 好友对象具有姓名、地址和电话号码,但我只想过滤其姓名。在我的活动中...
searchBarTextView.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
friendListAdapter.getFilter().filter(s);
}}
在适配器中时...
@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;
}
有人可以帮助我如何正确显示过滤后的阵列适配器吗?我认为notifyDataSetChanged 没有被调用。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的问题解决了,发现我必须重写 getCount() 和 getItem() 。
My problem is solved, found out that I have to override getCount() and getItem().