【问题标题】:Cannot resolve method getFilter() method无法解析方法 getFilter() 方法
【发布时间】:2017-11-10 21:58:56
【问题描述】:

它显示相同的错误,但是我尝试实现此功能。我不明白是什么造成了问题,因为它只是课堂上的另一个功能。 我正在实现这个以将 SearchView 与 RecyclerAdapter 一起使用。我见过其他代码,但都以相同的类型实现。 这是full code

class ProductsRecyclerAdapter extends RecyclerView.Adapter<ProductsRecyclerAdapter.MyViewHolder> implements Filterable {

    List<ProductEntity> productList = Collections.emptyList();
    Context context;
    List<ProductEntity> filteredProductList = Collections.emptyList();

    public ProductsRecyclerAdapter(List<ProductEntity> productList, Context context) {
        this.productList = productList;
        this.context = context;
        filteredProductList = productList;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView product_name;
        ImageView product_image;
        CardView product_catalog_card;
        AVLoadingIndicatorView progress_ball_image ;

        public MyViewHolder(View view) {
            super(view);
            product_name = view.findViewById(R.id.product_name);
            product_image = view.findViewById(R.id.product_image);
            product_catalog_card = view.findViewById(R.id.product_catalog_card);
            progress_ball_image = view.findViewById(R.id.progress_ball_image) ;
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_entity_design, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.product_name.setText(productList.get(position).about);
        holder.progress_ball_image.setVisibility(View.VISIBLE) ;
        Picasso.with(context)
                .load(productList.get(position).image.get(0).url)
                .centerInside()
                .resize(200, 200)
                .memoryPolicy(MemoryPolicy.NO_STORE)
                .into(holder.product_image, new Callback() {
                    @Override
                    public void onSuccess() {
                        holder.progress_ball_image.smoothToHide(); ;
                    }

                    @Override
                    public void onError() {

                    }
                });
        holder.product_catalog_card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), ProductDetail.class);
                intent.putExtra("product", productList.get(position));
                v.getContext().startActivity(intent);
            }
        });
    }

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

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String charString = charSequence.toString();
                if (charString.isEmpty()) {
                    filteredProductList = productList;
                } else {
                    ArrayList<ProductEntity> filteredList = new ArrayList<>();
                    for (ProductEntity productEntity : productList) {
                        if (productEntity.about.toLowerCase().contains(charString)) {
                            filteredList.add(productEntity);
                        }
                    }
                    filteredProductList = filteredList;
                }

                FilterResults filterResults = new FilterResults();
                filterResults.values = filteredProductList;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                filteredProductList = (ArrayList<ProductEntity>) filterResults.values;
                notifyDataSetChanged();
            }
        };
    }
}

This is the error snapshot

【问题讨论】:

  • 我可以看到您的 ProductsRecyclerAdapter 类具有包可见性。您是否尝试在同一个包中使用此适配器?
  • 是的,这有什么问题吗?如果你想看的话,我已经在上面的链接中发布了我的全部内容。我刚刚通过创建另一个类来检查它,但这没有帮助。

标签: java android android-recyclerview android-filterable


【解决方案1】:

Rahul Mishra,看,您将变量 productsRecyclerAdapter 声明为 RecyclerView.Adapter,但您需要 ProductsRecyclerAdapter。更改变量的类型,它将起作用

【讨论】:

  • 真是个愚蠢的错误。我正在为它沉思了好几个小时。非常感谢您的帮助。
猜你喜欢
  • 2020-07-09
  • 2015-05-29
  • 1970-01-01
  • 1970-01-01
  • 2016-01-15
  • 2015-06-10
  • 2019-08-28
  • 2019-10-10
  • 2019-06-13
相关资源
最近更新 更多