android-fragments 如何在Android片段中使用filter.getFilter().filter(newtext)方法

5gfr0r5j  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(145)

我在一个片段中使用了一个RecyclerView
问题是我不能在片段中使用adapter.getfilter().filter(newText)

错误----〉不能从静态上下文引用非静态方法'getFilter()'

  • 请参阅以下代码以供参考 *:
    片段代码:-
@Override
        public boolean onQueryTextChange(String newText) {
            adapterRecVHomeFrag.getFilter().filter(newText);
            return false;

        }
    });

适配器代码

public class adapterRecVHomeFrag  extends  RecyclerView.Adapter<adapterRecVHomeFrag.myviewholder> implements Filterable {

    ArrayList<dataModelRecVFragHome> dataHolder;
    ArrayList<dataModelRecVFragHome> dataHolderBackup;

    public adapterRecVHomeFrag(ArrayList<dataModelRecVFragHome> dataHolder) {
        this.dataHolder = dataHolder;
        dataHolderBackup = new ArrayList<>(dataHolder);
    }

    @NonNull
    @Override
    public myviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new myviewholder(LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row_design_rec_homef
                                    ,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull myviewholder holder, int position) {
        holder.img.setImageResource(dataHolder.get(position).getImage());
        holder.title.setText(dataHolder.get(position).getTitle());
        holder.desc.setText(dataHolder.get(position).getDescription());
        holder.likeImg.setImageResource(dataHolder.get(position).getLikeImg());
        holder.commentImg.setImageResource(dataHolder.get(position).getCommentImg());
    }

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

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

    Filter filter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence keyword) {
            ArrayList<dataModelRecVFragHome> filteredData = new ArrayList<>();

            if(keyword.toString().isEmpty() || keyword.length() == 0){
                filteredData.addAll(dataHolderBackup);
            }else{
                for(dataModelRecVFragHome obj : dataHolderBackup){
                    if(obj.getTitle().toString().toLowerCase().trim().contains(keyword
                                .toString().toLowerCase().trim()) ||
                        obj.getDescription().toString().toLowerCase().trim().contains(keyword
                                .toString().toLowerCase().trim()))
                    {
                        filteredData.add(obj);
                    }
                }
            }

            FilterResults results = new FilterResults();
            results.values=filteredData;
            return  results;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            dataHolderBackup.clear();
            dataHolderBackup.addAll((ArrayList<dataModelRecVFragHome>)filterResults.values);
            notifyDataSetChanged();
        }
    };

    class myviewholder extends RecyclerView.ViewHolder{

        ImageView img,likeImg,commentImg;
        TextView title,desc;

        public myviewholder(@NonNull View itemView) {
            super(itemView);

            img = itemView.findViewById(R.id.imageVSingleRowDesRecHomeF);
            title = itemView.findViewById(R.id.titleTxtVSingleRDesRecHomeF);
            desc = itemView.findViewById(R.id.descriptionTxtVSingleRDesRecHomeF);
            likeImg = itemView.findViewById(R.id.likeImgVSingleRDesRecHomeF);
            commentImg = itemView.findViewById(R.id.commentImgVSingleRDesRecHomeF);

            likeImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(itemView.getContext(), "Liked", Toast.LENGTH_SHORT).show();
                }
            });

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                // Apply activity transition
                commentImg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(view.getContext(), "Clicked", Toast.LENGTH_SHORT).show();
                        Intent i = new Intent(view.getContext(),commentSc.class);
                        itemView.getContext().startActivity(i);
                    }
                });
            } else {
                // Swap without transition
                commentImg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(view.getContext(), "clicked", Toast.LENGTH_SHORT).show();
                        Intent i = new Intent(view.getContext(),commentSc.class);
                        itemView.getContext().startActivity(i);
                    }
                });
            }

        }//End myViewHolder
    }//End classMyViewHolder
}
plicqrtu

plicqrtu1#

答案
答案---〉我忘了创建适配器类的示例
解决方案-----〉

adapterRecVHomeFrag adapter;

添加此行以创建适配器类类型的变量。

相关问题