无法解析方法getFilter()方法

时间:2017-11-10 21:58:57

标签: java android android-recyclerview android-filterable

它显示相同的错误,但我尝试实现此功能。我不明白什么是创造问题,因为它只是课堂上的另一个功能。 我正在实现这个使用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

1 个答案:

答案 0 :(得分:1)

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