在ListView中搜索特定描述时,SearchView不会刷新
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在ListView中搜索特定描述时,SearchView不会刷新相关的知识,希望对你有一定的参考价值。
我正在为我的拍卖列表实现搜索功能。我可以过滤我在SearchView中输入的内容,但是当我删除字段时,它不会刷新。为什么会这样?我相信我的代码已经在onQueryTextChange中考虑了这一点。请指教。
auction list.Java:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.searchmenu,menu);
final 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) {
if (TextUtils.isEmpty(newText)) {
mListView.clearTextFilter();
mAdapter.getFilter().filter("");
} else {
mAdapter.getFilter().filter(newText);
}
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
auction adapter.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<>();
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();
}
}
private class ViewHolder{
ImageView imageView;
TextView txtName,txtDescription,txtPrice,txtDuration;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = new ViewHolder();
if(row == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layout,null);
holder.txtName=row.findViewById(R.id.txtName);
holder.txtDescription=row.findViewById(R.id.txtDescription);
holder.txtPrice=row.findViewById(R.id.txtPrice);
holder.txtDuration=row.findViewById(R.id.txtDuration);
holder.imageView=row.findViewById(R.id.imgIcon);
row.setTag(holder);
}
else{
holder = (ViewHolder)row.getTag();
}
Model model = auctionList.get(position);
holder.txtName.setText(model.getName());
holder.txtDescription.setText(model.getDescription());
holder.txtPrice.setText(model.getPrice());
holder.txtDuration.setText(model.getDuration());
byte[] auctionImage = model.getImage();
Bitmap bitmap = BitmapFactory.decodeByteArray(auctionImage,0,auctionImage.length);
holder.imageView.setImageBitmap(bitmap);
return row;
}
}
答案
我在您的代码中进行了一些更改。请查看更改,如果您仍然遇到任何问题,请告诉我们。
auction list.Java:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.searchmenu, menu);
final 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 true;
}
});
return super.onCreateOptionsMenu(menu);
}
auction adapter.Java:
public class AuctionListAdapter extends BaseAdapter implements Filterable {
private Context context;
private int layout;
private ArrayList<Model> auctionList,filterAuctionAl;
public AuctionListAdapter() {
}
public AuctionListAdapter(Context context, int layout, ArrayList<Model> auctionList) {
this.context = context;
this.layout = layout;
this.auctionList = auctionList;
filterAuctionAl = auctionList;
}
@Override
public int getCount() {
return filterAuctionAl.size();
}
@Override
public Object getItem(int position) {
return filterAuctionAl.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Filter getFilter() {
return new ValueFilter();
}
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
String content= constraint.toString().toUpperCase();
ArrayList<Model> filterList = new ArrayList<>();
for (int i = 0; i < filterAuctionAl.size(); i++) {
if ((filterAuctionAl.get(i).getName().toUpperCase().contains(content))
||(filterAuctionAl.get(i).getDescription().toUpperCase().contains(content))
||(filterAuctionAl.get(i).getPrice().toUpperCase().contains(content))
||(filterAuctionAl.get(i).getDuration().toUpperCase().contains(content))) {
Model model = new Model(filterAuctionAl.get(i).getId(),filterAuctionAl.get(i).getName(),
filterAuctionAl.get(i).getDescription(),filterAuctionAl.get(i).getPrice(),filterAuctionAl.get(i).getDuration()
,filterAuctionAl.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) {
filterAuctionAl = (ArrayList<Model>) results.values;
notifyDataSetChanged();
}
}
private class ViewHolder{
ImageView imageView;
TextView txtName,txtDescription,txtPrice,txtDuration;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = new ViewHolder();
if(row == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layout,null);
holder.txtName=row.findViewById(R.id.txtName);
holder.txtDescription=row.findViewById(R.id.txtDescription);
holder.txtPrice=row.findViewById(R.id.txtPrice);
holder.txtDuration=row.findViewById(R.id.txtDuration);
holder.imageView=row.findViewById(R.id.imgIcon);
row.setTag(holder);
}
else{
holder = (ViewHolder)row.getTag();
}
Model model = filterAuctionAl.get(position);
holder.txtName.setText(model.getName());
holder.txtDescription.setText(model.getDescription());
holder.txtPrice.setText(model.getPrice());
holder.txtDuration.setText(model.getDuration());
byte[] auctionImage = model.getImage();
Bitmap bitmap = BitmapFactory.decodeByteArray(auctionImage,0,auctionImage.length);
holder.imageView.setImageBitmap(bitmap);
return row;
}
}
如有任何疑问,请告诉我!
谢谢。
以上是关于在ListView中搜索特定描述时,SearchView不会刷新的主要内容,如果未能解决你的问题,请参考以下文章