我的列表视图不会过滤活动结果。当我输入 vales在我的适配器中成功过滤。我通过运行以下代码中的日志来检查它。在日志中显示过滤的值。 但我没有看到活动的任何变化
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
listData = (ArrayList<ContactLists>) results.values;
Log.e("FILTERED: ",results.values.toString()); // filter results appear here
notifyDataSetChanged();
}
但我的活动不会过滤列表视图。我的活动代码
ContactListAdapter myAdapter;
ArrayList<ContactLists> listData;
...
...
public void getContacts(){
listData = getListData();
myAdapter = new ContactListAdapter(this, listData);
listView = (ListView) findViewById(R.id.custom_list);
listView.setAdapter(new ContactListAdapter(this, listData));
}
public boolean onQueryTextChange(String newText) {
myAdapter.getFilter().filter(newText);
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
CustomAdapter
ArrayList<ContactLists> listData;
ArrayList<ContactLists> mStringFilterList;
ValueFilter valueFilter;
...
...
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<ContactLists> filterList = new ArrayList<ContactLists>();
for (int i = 0; i < mStringFilterList.size(); i++) {
if ((mStringFilterList.get(i).getName().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
ContactLists mydata = new ContactLists(mStringFilterList.get(i)
.getName(), mStringFilterList.get(i)
.getNumber());
filterList.add(mydata);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = mStringFilterList.size();
results.values = mStringFilterList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
listData = (ArrayList<ContactLists>) results.values;
Log.e("XXXXXXXXX",results.values.toString());
notifyDataSetChanged();
}
}
答案 0 :(得分:1)
试试这个
public boolean onQueryTextChange(String newText) { myAdapter.getFilter().filter(newText);
Listview.setAdapter(myAdapter);return false; }