过滤器适配器不显示过滤器结果 Android
Filter Adapter not displaying the filter results Android
我的列表视图不会过滤 activity 上的结果。当我输入
山谷在我的适配器中成功过滤。我通过 运行 下面代码中的日志检查了它。在日志中它显示过滤值。
但是我没有看到 activity
有任何变化
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
listData = (ArrayList<ContactLists>) results.values;
Log.e("FILTERED: ",results.values.toString()); // filter results appear here
notifyDataSetChanged();
}
但是我的 activity 没有过滤列表视图。我的 activity 代码如下
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;
}
自定义适配器
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();
}
}
试试这个
public boolean onQueryTextChange(String newText) { myAdapter.getFilter().filter(newText);<br>
Listview.setAdapter(myAdapter);return 假; }
我的列表视图不会过滤 activity 上的结果。当我输入 山谷在我的适配器中成功过滤。我通过 运行 下面代码中的日志检查了它。在日志中它显示过滤值。 但是我没有看到 activity
有任何变化@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
listData = (ArrayList<ContactLists>) results.values;
Log.e("FILTERED: ",results.values.toString()); // filter results appear here
notifyDataSetChanged();
}
但是我的 activity 没有过滤列表视图。我的 activity 代码如下
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;
}
自定义适配器
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();
}
}
试试这个
public boolean onQueryTextChange(String newText) { myAdapter.getFilter().filter(newText);<br>
Listview.setAdapter(myAdapter);return 假; }