gpt4 book ai didi

安卓 : Filterable not working on text clear

转载 作者:行者123 更新时间:2023-11-29 19:28:08 25 4
gpt4 key购买 nike

我有自定义 ListView ,我已经实现了 SearchView 来过滤数据。当我们为搜索 ListView 键入内容时,正确地获取过滤器,但是当我从搜索文本中清除某些字符时, ListView 不会过滤数据。它在最后的搜索位置保持不变。

让我用截图来解释。

这里,List 还没有被过滤。所有项目都存在。我正在搜索每个项目的两个字段(名称和描述)。

enter image description here

在这个屏幕上,我从 cam 搜索它显示了正确的结果。

enter image description here

我在 c 之前有明文,我希望结果应该是两个项目,即相机和 LED 电视。

enter image description here

这是我的代码。

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();
}
}
}

搜索查看代码

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;
}
});

请帮忙。

最佳答案

这是因为您正在修改 itemRateList

检查这个方法:

@Override
protected void publishResults(CharSequence charSequence, FilterResults results) {
itemRateList=(ArrayList<RateList>) results.values;
notifyDataSetChanged();
}

我会建议将结果存储在一个单独的 arrayList 中,并在您的适配器类中使用它。

由于在原来的itemRateList中更新了上面的结果,所以下次调用performFiltering方法时。您只是在迭代之前的结果,而不是原始列表。

希望这对您有所帮助。

关于安卓 : Filterable not working on text clear,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40896572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com