我有自定义列表视图,我已经实现了SearchView来过滤数据。当我们为搜索列表视图键入内容时,获取正确的过滤器,但是当我清除搜索文本中的某些字符时,listview不会过滤数据。它在最后一个搜索位置保持不变。
让我用截图解释。
此处,列表尚未过滤。所有项目都存在。 我在每个项目的两个字段(名称和描述)中搜索。
在此屏幕中,我搜索了cam
,它显示了正确的结果。
我有明确的文字直到c,我期待resut应该是两个项目,即相机和LED电视。
这是我的代码。
RateListAdapter.java
public class RateListAdapter extends BaseAdapter implements Filterable {
Context context;
private SparseBooleanArray mSelectedItemsIds;
public ArrayList<RateList> itemRateList;
public LayoutInflater inflater = null;
public ArrayList<RateList> tempList;
CustomFilter filter;
public RateListAdapter(Context context, ArrayList<RateList> itemRateList) {
this.context = context;
this.itemRateList = itemRateList;
mSelectedItemsIds = new SparseBooleanArray();
this.tempList = itemRateList;
}
@Override
public int getCount() {
return itemRateList.size();
}
@Override
public RateList getItem(int i) {
return itemRateList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
class Holder {
TextView tv_name, tv_description, tv_amount;
ImageView iv_itemImage;
}
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
Holder h = new Holder();
RateList rateList = itemRateList.get(i);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.rate_list_item, viewGroup, false);
}
h.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
h.tv_description = (TextView) convertView.findViewById(R.id.tv_description);
h.tv_amount = (TextView) convertView.findViewById(R.id.tv_amount);
h.iv_itemImage = (ImageView) convertView.findViewById(R.id.iv_itemImage);
h.tv_name.setText(rateList.Name);
h.tv_description.setText(rateList.description);
h.tv_amount.setText("₹ "+rateList.amount);
Picasso.with(context).load(rateList.image).resize(92,92).into(h.iv_itemImage);
return convertView;
}
public void toggleSelection(int position) {
selectView(position, !mSelectedItemsIds.get(position));
}
public void selectView(int position, boolean value) {
if (value)
mSelectedItemsIds.put(position, value);
else
mSelectedItemsIds.delete(position);
notifyDataSetChanged();
}
public SparseBooleanArray getSelectedIds() {
return mSelectedItemsIds;
}
public void removeSelection() {
mSelectedItemsIds = new SparseBooleanArray();
notifyDataSetChanged();
}
@Override
public Filter getFilter() {
if(filter == null)
{
filter=new CustomFilter();
}
return filter;
}
private class CustomFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results=new FilterResults();
if(constraint != null && constraint.length()>0) {
constraint=constraint.toString().toLowerCase();
ArrayList<RateList> filters=new ArrayList<RateList>();
for(int i=0;i<itemRateList.size();i++) {
if (itemRateList.get(i).Name.toLowerCase().contains(constraint) ||
itemRateList.get(i).description.toLowerCase().contains(constraint)) {
RateList r = new RateList();
r.Name = itemRateList.get(i).Name;
r.description= itemRateList.get(i).description;
r.id = itemRateList.get(i).id;
r.image = itemRateList.get(i).image;
r.amount = itemRateList.get(i).amount;
filters.add(r);
}
}
results.count = filters.size();
results.values = filters;
} else {
results.count=itemRateList.size();
results.values=itemRateList;
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults results) {
itemRateList=(ArrayList<RateList>) results.values;
notifyDataSetChanged();
}
}
}
的searchview代码
BookingFrament.java
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String searchQuery) {
rateListAdapter.getFilter().filter(searchQuery);
return false;
}
});
请帮忙。
答案 0 :(得分:1)
这是因为您正在修改itemRateList
检查此方法:
@Override
protected void publishResults(CharSequence charSequence, FilterResults results) {
itemRateList=(ArrayList<RateList>) results.values;
notifyDataSetChanged();
}
我建议将结果存储在一个单独的arrayList中,并将其用于适配器类。
由于上述结果在原始itemRateList
中更新,因此下次调用performFiltering
方法时。您只是迭代上一个结果而不是原始列表。
希望这有帮助。