問題描述
我有一個作為搜索欄的編輯文本和一個過濾我輸入的文本的列表視圖,但不幸的是,它不會過濾列表視圖.我使用了帶有對象 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有人可以幫我如何正確顯示過濾后的數組適配器嗎?我認為 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().
這篇關于從編輯文本中過濾列表視圖的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!