gpt4 book ai didi

java - 使用搜索 View 在 recyclerview 中过滤 ListItem 不起作用

转载 作者:行者123 更新时间:2023-12-01 22:37:46 26 4
gpt4 key购买 nike

我想使用 searchview 过滤从 List 中的 json 获取的结果

我尝试过使用 Filter getFilter() 方法,但似乎不起作用。

这是我的适配器类

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> implements Filterable {



public List<ListItem> listItems;
public Context context;
public List<ListItem> AllBanks;



public MyAdapter(List<ListItem> listItemAdap, Context context) {
this.listItems = listItemAdap;
this.context = context;
this.AllBanks = new ArrayList<>(listItemAdap);



}

@Override
public ViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_items,parent,false);
return new ViewHolder(v);


}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
ListItem listItem = listItems.get(position);
holder.txtviewBankName.setText(listItem.getBank_name());
holder.txtviewAddress.setText(listItem.getAddress());
holder.txtviewIfsc.setText(listItem.getIfsc());
holder.txtviewBranch.setText(listItem.getBranch());
holder.txtviewBankId.setText(listItem.getBank_id());
holder.txtviewCity.setText(listItem.getCity());
holder.txtviewDistrict.setText(listItem.getDistrict());
holder.txtviewState.setText(listItem.getState());



}
@Override
public Filter getFilter() {
return filter;
}

@Override
public int getItemCount() {
return listItems.size();
}




private Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {

List<ListItem> filteredList = new ArrayList<>();

if (charSequence.toString().isEmpty())
{
filteredList.addAll(AllBanks);
}
else
{
for (ListItem Banks: AllBanks)
{
if ( Banks.toString().toLowerCase().contains(charSequence.toString().toLowerCase()))
{
filteredList.add(Banks);
}
}
}

FilterResults filterResults = new FilterResults();
filterResults.values = filteredList;

return filterResults;
}

@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {

listItems.clear();
listItems.addAll((Collection<? extends ListItem>) filterResults.values);
notifyDataSetChanged();

}
};

public class ViewHolder extends RecyclerView.ViewHolder
{
public TextView txtviewBankName;
public TextView txtviewAddress;
public TextView txtviewIfsc;
public TextView txtviewBranch;
public TextView txtviewBankId;
public TextView txtviewCity;
public TextView txtviewDistrict;
public TextView txtviewState;

public ViewHolder(View itemView) {
super(itemView);

txtviewBankName = (TextView)itemView.findViewById(R.id.bankName);
txtviewAddress = (TextView)itemView.findViewById(R.id.address);
txtviewIfsc = (TextView)itemView.findViewById(R.id.ifsc);
txtviewBranch = (TextView)itemView.findViewById(R.id.branch);
txtviewBankId = (TextView)itemView.findViewById(R.id.bankId);
txtviewCity = (TextView)itemView.findViewById(R.id.city);
txtviewDistrict = (TextView)itemView.findViewById(R.id.district);
txtviewState = (TextView)itemView.findViewById(R.id.state);
}
}
}

这是我的 ListItem 类

公共(public)类ListItem {

private String bank_name;
private String address;
private String ifsc;
private String branch;
private String bank_id;
private String city;
private String district;
private String state;

public ListItem(String bank_name, String address, String ifsc, String branch, String bank_id, String city, String district, String state)
{
this.bank_name = bank_name;
this.address = address;
this.ifsc = ifsc;
this.branch = branch;
this.bank_id = bank_id;
this.city = city;
this.district = district;
this.state = state;
}

public String getBank_name()
{
return bank_name;
}

public String getAddress() {
return address;
}

public String getIfsc() {
return ifsc;
}

public String getBranch() {
return branch;
}

public String getBank_id() {
return bank_id;
}

public String getCity() {
return city;
}

public String getDistrict() {
return district;
}

public String getState() {
return state;
}

}

我希望当我开始在搜索字段中输入内容时,会过滤获取的 ListView 。

最佳答案

也许 onCreateOptionsMenu 和 Filter getFilter 部分可以在此站点上提供帮助

http://programmingroot.com/android-recyclerview-search-filter-tutorial-with-example/

关于java - 使用搜索 View 在 recyclerview 中过滤 ListItem 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58516667/

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