我实现了一个searchview来过滤从api接收的数据,该数据在searchview中搜索之前可以有效显示,第一个问题,提交要过滤的文本后我没有任何数据,其次当我单击返回时在searchview上,我之前在cardview布局上获得的数据都消失了。
为searchview实施过滤器
{
public Filter getFilter() {
return myplan;
}
Filter myplan = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
ArrayList<modelclass> array = new ArrayList<>();
if(constraint == null || constraint.length() == 0)
{
array.addAll(arrayListfull);
}else {
String pattern = array.toString().toLowerCase().trim();
for (modelclass myclass : array){
if(myclass.getPlayer().toLowerCase().contains(pattern)){
array.add(myclass);
}
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = array;
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
arrayListi.clear();
arrayListi =(ArrayList<modelclass>) results.values;
notifyDataSetChanged();
}
};
}
{
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menumenu,menu);
MenuItem item = menu.findItem(R.id.search);
SearchView searchView = (SearchView)item.getActionView();
searchView.setQueryHint("Search...");
searchView.setOnQueryTextListener(new
SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return true;
}
});
return true;
}
}