当前,我的代码中没有错误,但是每当我在ListView中搜索一个项目时,它都可以正常工作,但是当我后退/删除SearchView中的输入时,它不会刷新。我该如何编码此刷新部分?
就像我每次在SearchView中键入或删除内容一样,它实际上应该通过在ListView中显示适当的结果来不断刷新我的搜索。例如,如果我完全删除SearchView中的输入,它将显示所有可用的结果(当前,我的不会刷新)
AuctionListAdapter.java:
public class AuctionListAdapter extends BaseAdapter implements Filterable {
ValueFilter valueFilter;
private Context context;
private int layout;
private ArrayList<Model> auctionList;
public AuctionListAdapter(Context context, int layout, ArrayList<Model> auctionList) {
this.context = context;
this.layout = layout;
this.auctionList = auctionList;
}
@Override
public int getCount() {
return auctionList.size();
}
@Override
public Object getItem(int position) {
return auctionList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
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<Model> filterList = new ArrayList<Model>();
for (int i = 0; i < auctionList.size(); i++) {
if ((auctionList.get(i).getName().toUpperCase())
.contains(constraint.toString().toUpperCase())||(auctionList.get(i).getDescription().toUpperCase())
.contains(constraint.toString().toUpperCase())||(auctionList.get(i).getPrice().toUpperCase())
.contains(constraint.toString().toUpperCase())||(auctionList.get(i).getDuration().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
Model model = new Model(auctionList.get(i).getId(),auctionList.get(i).getName(),
auctionList.get(i).getDescription(),auctionList.get(i).getPrice(),auctionList.get(i).getDuration()
,auctionList.get(i).getImage());
filterList.add(model);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = auctionList.size();
results.values = auctionList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
auctionList = (ArrayList<Model>) results.values;
notifyDataSetChanged();
}
}
}
AuctionList.java:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.searchmenu,menu);
MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView)myActionMenuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String text) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
mAdapter.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}